Guest User

penis

a guest
Feb 27th, 2020
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1.  
  2.  
  3. void * thread2(void *args) {
  4.  
  5. while (1) {
  6.  
  7. pthread_mutex_lock(&lock);
  8.  
  9. if (counter <= MAX_COUNTER_LIMIT) {
  10.  
  11. sum = sum + counter;
  12.  
  13. counter++;
  14.  
  15. }
  16.  
  17. else {
  18.  
  19. pthread_mutex_unlock(&lock);
  20.  
  21. break;
  22.  
  23. }
  24.  
  25. pthread_mutex_unlock(&lock);
  26.  
  27. }
  28.  
  29. }
  30.  
  31. /* Similar to thread1 function */
  32.  
  33. void * thread3(void *args) {
  34.  
  35. while (1) {
  36.  
  37. pthread_mutex_lock(&lock);
  38.  
  39. if (counter <= MAX_COUNTER_LIMIT) {
  40.  
  41. sum = sum + counter;
  42.  
  43. counter++;
  44.  
  45. }
  46.  
  47. else {
  48.  
  49. pthread_mutex_unlock(&lock);
  50.  
  51. break;
  52.  
  53. }
  54.  
  55. pthread_mutex_unlock(&lock);
  56.  
  57. }
  58.  
  59. }
  60.  
  61. /* Similar to thread1 function */
  62.  
  63. void * thread4(void *args) {
  64.  
  65. while (1) {
  66.  
  67. pthread_mutex_lock(&lock);
  68.  
  69. if (counter <= MAX_COUNTER_LIMIT) {
  70.  
  71. sum = sum + counter;
  72.  
  73. counter++;
  74.  
  75. }
  76.  
  77. else {
  78.  
  79. pthread_mutex_unlock(&lock);
  80.  
  81. break;
  82.  
  83. }
  84.  
  85. pthread_mutex_unlock(&lock);
  86.  
  87. }
  88.  
  89. }
  90.  
  91. int main() {
  92.  
  93. /* Declaring 4 thread variables */
  94.  
  95. pthread_t t1, t2, t3, t4;
  96.  
  97. /* Initializing mutex lock */
  98.  
  99. if (pthread_mutex_init(&lock, NULL) != 0) {
  100.  
  101. cout << "Mutex init failed\n";
  102.  
  103. return 1;
  104.  
  105. }
  106.  
  107.  
  108.  
  109. /* Creating thread1 which run thread1() function */
  110.  
  111. pthread_create(&t1, NULL, thread1, NULL);
  112.  
  113.  
  114.  
  115. /* Creating thread2 which run thread2() function */
  116.  
  117. pthread_create(&t2, NULL, thread2, NULL);
  118.  
  119.  
  120.  
  121. /* Creating thread3 which run thread3() function */
  122.  
  123. pthread_create(&t3, NULL, thread3, NULL);
  124.  
  125.  
  126.  
  127. /* Creating thread4 which run thread4() function */
  128.  
  129. pthread_create(&t4, NULL, thread4, NULL);
  130.  
  131.  
  132.  
  133. /* Joining all threads to main thread, so that it waits until 4 threads complete
  134.  
  135. * their execution */
  136.  
  137. pthread_join(t1, NULL);
  138.  
  139. pthread_join(t2, NULL);
  140.  
  141. pthread_join(t3, NULL);
  142.  
  143. pthread_join(t4, NULL);
  144.  
  145.  
  146.  
  147. /* destroying mutex lock */
  148.  
  149. pthread_mutex_destroy(&lock);
  150.  
  151.  
  152.  
  153. /* Printing the final counter value and and Sum */
  154.  
  155.  
  156. cout << ("Final Sum added by 4 threads: %lld\n", sum);
  157.  
  158.  
  159. return 0;
  160.  
  161. }
Advertisement
Add Comment
Please, Sign In to add comment