Advertisement
Guest User

Untitled

a guest
Feb 6th, 2016
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.08 KB | None | 0 0
  1. #include <pthread.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <unistd.h>
  5. #include <stdbool.h>
  6.  
  7. #define WORKERS 2
  8. #define ARRAY_ELEMENTS 100
  9. #define MAX 1000
  10.  
  11.  
  12. pthread_mutex_t mutex_bucket1 = PTHREAD_MUTEX_INITIALIZER;
  13. pthread_mutex_t mutex_signal = PTHREAD_MUTEX_INITIALIZER;
  14. pthread_cond_t cond_go = PTHREAD_COND_INITIALIZER;
  15. pthread_cond_t cond_busy = PTHREAD_COND_INITIALIZER;
  16.  
  17. static int value = 0;
  18. bool available = false;
  19.  
  20. void *worker_thread(void *pbucket)
  21. {
  22. sleep(5);
  23. while(1)
  24. {
  25. unsigned int count = 0;
  26. int local_array[ARRAY_ELEMENTS];
  27. int *ptbucket = (int*)pbucket;
  28. setbuf(stdout, NULL);
  29.  
  30. pthread_mutex_lock(&mutex_signal);
  31. printf(" -------------- n chainging state to available n --------- ");
  32. available = true;
  33. printf(" -------------- n from thread sending go signal n --------- ");
  34. pthread_cond_signal(&cond_go);
  35. pthread_mutex_unlock(&mutex_signal);
  36.  
  37.  
  38. pthread_mutex_lock(&mutex_bucket1);
  39. printf(" -------------- n data part locked in thread for copying n --------- ");
  40. while(count < ARRAY_ELEMENTS)
  41. {
  42. printf(" %d - n", ptbucket[count]); /***incorrect values***/
  43. local_array[count] = ptbucket[count];
  44. count++;
  45. }
  46. pthread_mutex_unlock(&mutex_bucket1);
  47.  
  48. /*Never able to acquire mutex_signal and change state to not available*/ **BUG**
  49. pthread_mutex_lock(&mutex_signal);
  50. printf(" -------------- n chainging state to not available n --------- ");
  51. available = false;
  52. pthread_mutex_unlock(&mutex_signal);
  53.  
  54. count = 0;
  55.  
  56. while(count < ARRAY_ELEMENTS)
  57. {
  58. printf(" %d - n", local_array[count]);
  59. count++;
  60. }
  61.  
  62. printf(" -------------- n about to sleep for 5secs n --------- ");
  63. sleep(5);
  64. }
  65. }
  66.  
  67. int main(void)
  68. {
  69. pthread_t thread_id[WORKERS];
  70.  
  71. unsigned int* pbucket1 = (int*) malloc(sizeof(int) * ARRAY_ELEMENTS);
  72.  
  73. unsigned int* pbucket;
  74.  
  75. for(int i = 0; i < WORKERS - 1; i++)
  76. {
  77. pthread_create(&thread_id[i], NULL, worker_thread, (void *) pbucket);
  78. }
  79.  
  80. for(int i = 0; i < MAX; i++)
  81. {
  82. unsigned int count = 0;
  83.  
  84. pbucket = pbucket1;
  85.  
  86. // Make the payload ready
  87. pthread_mutex_lock(&mutex_bucket1);
  88.  
  89. printf(" -------------- creating data payload --------- n");
  90.  
  91. while(count < ARRAY_ELEMENTS)
  92. {
  93. pbucket1[count] = i;
  94. i++;
  95. count++;
  96. }
  97.  
  98. printf(" -------------- n waiting for go signal n --------- ");
  99.  
  100. while(!available)
  101. {
  102. pthread_cond_wait(&cond_go, &mutex_signal);
  103. }
  104.  
  105. pthread_mutex_unlock(&mutex_bucket1);
  106.  
  107. /*I believe after we unlock variable "available" can be mutexed
  108. again by other thread but seems thinking is flawed */
  109.  
  110. printf(" -------------- n Main thread sleep for 3 seconds n --------- ");
  111. sleep(3);
  112. }
  113.  
  114. for(int i = 0; i < WORKERS; i++)
  115. {
  116. pthread_join(thread_id[i], NULL);
  117. }
  118.  
  119. return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement