Advertisement
Guest User

Untitled

a guest
Feb 10th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.85 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. // This program increments the value of x in a new thread, thread_zero, after with the pthread_join we make sure the program continues after thread has been completed.
  4.  
  5. void *thread_function(void *param)
  6. {
  7. int *tp = (int *) param ;
  8. int j = 0;
  9. printf("Starting thread. *tp = %d\n",*tp);
  10.  
  11. for(j = 0; j<5; j++){
  12. printf("thread loop %d\n",*tp);
  13. *tp = *tp +1 ;
  14. }
  15. printf("thread completed *tp = %d\n",*tp);
  16.  
  17. }
  18.  
  19. int main(int argc,char *argv[])
  20. {
  21.  
  22. int x = 0,y = 10;
  23.  
  24. pthread_t thread_zero;
  25. pthread_create(&thread_zero,NULL,thread_function,&x);
  26. pthread_join(thread_zero,NULL);
  27.  
  28. printf("x+y = %d\n",x+y);
  29. pthread_exit(thread_zero);
  30.  
  31. return (0);
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement