futextest.pthread.c 996 B

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