futextest.pthread.c 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdio.h>
  4. #include <pthread.h>
  5. #include <unistd.h>
  6. void * print1 (void *arg)
  7. {
  8. printf("This is Function 1 - go to sleep\n");
  9. sleep(5);
  10. printf("Function1 out of sleep\n");
  11. printf("%s",(char *) arg);
  12. return NULL;
  13. }
  14. void * print2(void * arg)
  15. {
  16. printf("This is Function 2 - go to sleep\n");
  17. sleep(5);
  18. printf("Function2 out of sleep\n");
  19. printf("%s",(char *) arg);
  20. return NULL;
  21. }
  22. void * func(void * arg )
  23. {
  24. int * ptr = (int *) arg;
  25. printf("Parent gave %d\n",*ptr);
  26. }
  27. int main(int argc, char ** argv)
  28. {
  29. pthread_t thread1, thread2,thread3;
  30. int intvar = 12;
  31. pthread_create(&thread1, NULL, print1, "Thread1 Executing ...\n");
  32. pthread_create(&thread2, NULL, print2, "Thread2 Executing ...\n");
  33. pthread_create(&thread3, NULL, func, &intvar);
  34. printf("going to sleep\n");
  35. printf("out of sleep\n");
  36. pthread_join(thread1, NULL);
  37. pthread_join(thread2, NULL);
  38. pthread_join(thread3, NULL);
  39. return 0;
  40. }