Advertisement
Guest User

Untitled

a guest
Dec 20th, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstdlib>
  3. #include <pthread.h>
  4.  
  5. using namespace std;
  6.  
  7. #define NUM_THREADS 5
  8.  
  9. void *PrintHello(void *threadid)
  10. {
  11. long tid;
  12. tid = (long)threadid;
  13. cout << "Hello World! Thread ID, " << tid << endl;
  14. pthread_exit(NULL);
  15. }
  16.  
  17. int main ()
  18. {
  19. pthread_t threads[NUM_THREADS];
  20. int rc;
  21. int i;
  22. for( i=0; i < NUM_THREADS; i++ ){
  23. cout << "main() : creating thread, " << i << endl;
  24. rc = pthread_create(&threads[i], NULL,
  25. PrintHello, (void *)i);
  26. if (rc){
  27. cout << "Error:unable to create thread," << rc << endl;
  28. exit(-1);
  29. }
  30. }
  31. pthread_exit(NULL);
  32. }
  33.  
  34. the out put will be
  35. main() : creating thread, 0
  36. main() : creating thread, 1
  37. main() : creating thread, 2
  38. main() : creating thread, 3
  39. main() : creating thread, 4
  40. Hello World! Thread ID, 0
  41. Hello World! Thread ID, 1
  42. Hello World! Thread ID, 2
  43. Hello World! Thread ID, 3
  44. Hello World! Thread ID, 4
  45.  
  46. main() : creating thread, 0
  47. Hello World! Thread ID, 0
  48. main() : creating thread, 1
  49. Hello World! Thread ID, 1
  50. ....
  51.  
  52. Hello World! Thread ID, 1
  53. Hello World! Thread ID, 4
  54. Hello World! Thread ID, 0
  55. Hello World! Thread ID, 2
  56. Hello World! Thread ID, 3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement