futextest.pthread.c 1007 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <unistd.h>
  4. void * print1 (void *arg)
  5. {
  6. printf("This is Function 1 - go to sleep\n");
  7. sleep(5);
  8. printf("Function1 out of sleep\n");
  9. printf("%s",(char *) arg);
  10. return NULL;
  11. }
  12. void * print2(void * arg)
  13. {
  14. printf("This is Function 2 - go to sleep\n");
  15. sleep(5);
  16. printf("Function2 out of sleep\n");
  17. printf("%s",(char *) arg);
  18. return NULL;
  19. }
  20. void * func(void * arg )
  21. {
  22. int * ptr = (int *) arg;
  23. printf("Parent gave %d\n",*ptr);
  24. return NULL;
  25. }
  26. int main(int argc, char ** argv)
  27. {
  28. pthread_t thread1, thread2,thread3;
  29. int intvar = 12;
  30. pthread_create(&thread1, NULL, print1, "Thread1 Executing ...\n");
  31. pthread_create(&thread2, NULL, print2, "Thread2 Executing ...\n");
  32. pthread_create(&thread3, NULL, func, &intvar);
  33. printf("going to sleep\n");
  34. printf("out of sleep\n");
  35. pthread_join(thread1, NULL);
  36. pthread_join(thread2, NULL);
  37. pthread_join(thread3, NULL);
  38. return 0;
  39. }