Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.84 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #define NUM_THREADS 5
  5. void *perform_work(void *argument)
  6. {
  7. int passed_in_value;
  8. passed_in_value = *((int *) argument) ;
  9. printf ("Hello World! It's me.thread with arguent \d!\n", passed_in_value) ;
  10. }
  11.  
  12. int main (void)
  13. {
  14. pthread_t threads[NUM_THREADS];
  15. int thread_args[NUM_THREADS];
  16. int result_code, index;
  17.  
  18. for (index = 0; index < NUM_THREADS; ++index)
  19. {
  20. thread_args[index]= index;
  21. printf ("In main :creating thread %d\n", index);
  22. result_code = pthread_create(&threads[index], NULL, perform_work, (void *)&thread_args[index]);
  23. }
  24.  
  25. for (index = 0; index < NUM_THREADS; ++index) {
  26.  
  27. result_code = pthread_join(threads[index], NULL);
  28. printf ("In main :thread %d has cpleted\n", index);
  29. }
  30.  
  31. printf("In main:All threads Completed successfully\n");
  32. exit(EXIT_SUCCESS);
  33. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement