Advertisement
Guest User

Untitled

a guest
Jul 29th, 2015
168
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <unistd.h>
  5.  
  6. using namespace std;
  7.  
  8. void *print_message_function1(void *ptr);
  9. void *print_message_function2(void *ptr);
  10.  
  11. int main(){
  12. cout << "nMain: Creating threads" << endl;
  13. cout << "Main: Waiting for threads to finish" << endl << endl;
  14.  
  15. pthread_t thread1, thread2;
  16. char message1[] = " from Thread 1";
  17. char message2[] = " from Thread 2";
  18. int iret1, iret2;
  19.  
  20.  
  21.  
  22. iret1 = pthread_create( &thread1, 0, print_message_function1, (void*) message1);
  23. iret2 = pthread_create( &thread1, 0, print_message_function2, (void*) message2);
  24.  
  25.  
  26.  
  27. pthread_join(thread1, 0);
  28. pthread_join(thread2, 0);
  29.  
  30. cout << "Thread 0 terminates" << endl;
  31. cout << "Thread 1 terminates" << endl;
  32. cout << "Main: Exiting" << endl;
  33. exit(0);
  34. }
  35.  
  36.  
  37. void *print_message_function1(void *ptr){
  38. char *message;
  39. message = (char*) ptr;
  40. for(int i=0; i<10; i++){
  41. cout << "Hello #" << i << message << endl;
  42. sleep(1);
  43. }
  44. }
  45.  
  46. void *print_message_function2(void *ptr){
  47. char *message;
  48. message = (char*) ptr;
  49. for(int i=0; i<10; i++){
  50. cout << "Hello #" << i << message << endl;
  51. sleep(1);
  52. }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement