Guest User

Untitled

a guest
Jun 21st, 2018
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <pthread.h>
  4. #include <time.h>
  5.  
  6. void *print_message_function( void *ptr );
  7.  
  8. main()
  9. {
  10. pthread_t thread1, thread2;
  11. char *message1 = "A";
  12. char *message2 = "B";
  13. int iret1, iret2;
  14.  
  15. /* Create independent threads each of which will execute function */
  16.  
  17. iret1 = pthread_create( &thread1, NULL, print_message_function, (void*) message1);
  18. iret2 = pthread_create( &thread2, NULL, print_message_function, (void*) message2);
  19.  
  20. /* Wait till threads are complete before main continues. Unless we */
  21. /* wait we run the risk of executing an exit which will terminate */
  22. /* the process and all threads before the threads have completed. */
  23.  
  24. srand ( time(NULL) );
  25.  
  26. pthread_join( thread1, NULL);
  27. pthread_join( thread2, NULL);
  28.  
  29. printf("\nThread 1 returns: %d\n",iret1);
  30. printf("Thread 2 returns: %d\n",iret2);
  31. exit(0);
  32. }
  33.  
  34. void *print_message_function( void *ptr )
  35. {
  36. char *message;
  37. message = (char *) ptr;
  38. int i = 0;
  39. while(i!=6){
  40. i = rand()%1000;
  41. printf("%s", message);
  42. }
  43. }
Add Comment
Please, Sign In to add comment