c – 如何使用pthreads屏障?

前端之家收集整理的这篇文章主要介绍了c – 如何使用pthreads屏障?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
嗨很抱歉发布了一大堆代码,但我是C代码的新手,基本上我正在做大学的任务,我必须实现一个“pthread_barrier”,现在我理解了障碍的概念(或者在至少我认为我这样做但是我只是不确定我应该把它放在哪里.作业说明:

“使用pthread_barrier_init和pthread_barrier_wait确保所有生产者/消费者线程同时开始生产/消费.”

顺便说一下,这是作业的额外学分

  1. #include <pthread.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <stdio.h>
  5.  
  6. #define SIXTY_SECONDS 60000000
  7. #define ONE_SECOND 1000000
  8. #define RANGE 10
  9. #define PERIOD 2
  10.  
  11. typedef struct {
  12. int *carpark;
  13. int capacity;
  14. int occupied;
  15. int nextin;
  16. int nextout;
  17. int cars_in;
  18. int cars_out;
  19. pthread_mutex_t lock;
  20. pthread_cond_t space;
  21. pthread_cond_t car;
  22. pthread_barrier_t bar;
  23. } cp_t;
  24.  
  25. /* Our producer threads will each execute this function */
  26. static void *
  27. producer(void *cp_in)
  28. {
  29. cp_t *cp;
  30. unsigned int seed;
  31. /* Convert what was passed in to a pointer to a bounded buffer */
  32. cp = (cp_t *)cp_in;
  33.  
  34. /* Loop */
  35. while (1) {
  36. /* Sleep for up to 1s */
  37. usleep(rand_r(&seed) % ONE_SECOND);
  38. /* Acquire the lock */
  39. pthread_mutex_lock(&cp->lock);
  40. /* While full wait until there is room available */
  41. while (cp->occupied == cp->capacity) {
  42. pthread_cond_wait(&cp->car,&cp->lock);
  43. }
  44. /* Insert an item */
  45. cp->carpark[cp->nextin] = rand_r(&seed) % RANGE;
  46. /* Increment counters */
  47. cp->occupied++;
  48. cp->nextin++;
  49. cp->nextin %= cp->capacity;
  50. cp->cars_in++;
  51. /* Someone may be waiting on data to become available */
  52. pthread_cond_signal(&cp->space);
  53. /* Release the lock */
  54. pthread_mutex_unlock(&cp->lock);
  55. }
  56.  
  57. return ((void *)NULL);
  58. }
  59.  
  60. /* Our consumer threads will each execute this function */
  61. static void *
  62. consumer(void *cp_in)
  63. {
  64.  
  65. cp_t *cp;
  66. unsigned int seed;
  67. /* Convert what was passed in to a pointer to a bounded buffer */
  68. cp = (cp_t *)cp_in;
  69.  
  70. while (1) {
  71. /* Sleep for up to 1s */
  72. usleep(rand_r(&seed) % ONE_SECOND);
  73. /* Acquire the lock */
  74. pthread_mutex_lock(&cp->lock);
  75. /* While empty wait until there is data available */
  76.  
  77. while (cp->occupied == 0) {
  78. pthread_cond_wait(&cp->space,&cp->lock);
  79. }
  80.  
  81. /* Increment counters */
  82. cp->occupied--;
  83. cp->nextout++;
  84. cp->nextout %= cp->capacity;
  85. cp->cars_out++;
  86. /* Someone may be waiting on room to become available */
  87. pthread_cond_signal(&cp->car);
  88. /* Release the lock */
  89. pthread_mutex_unlock(&cp->lock);
  90. }
  91.  
  92. return ((void *)NULL);
  93. }
  94.  
  95. /* Our monitor thread will each execute this function */
  96. static void *
  97. monitor(void *cp_in)
  98. {
  99.  
  100. cp_t *cp;
  101. /* Convert what was passed in to a pointer to a bounded buffer */
  102. cp = (cp_t *)cp_in;
  103.  
  104. while (1) {
  105. /* Pause */
  106. sleep(PERIOD);
  107. /* Acquire the lock */
  108. pthread_mutex_lock(&cp->lock);
  109. printf("Delta: %d\n",cp->cars_in - cp->cars_out);
  110. /* Release the lock */
  111. pthread_mutex_unlock(&cp->lock);
  112. }
  113.  
  114. return ((void *)NULL);
  115. }
  116.  
  117. /* Initialisation */
  118. static int
  119. init(cp_t *cp,int capacity)
  120. {
  121.  
  122. /* Set up the bounded buffer internals */
  123. cp->occupied = cp->nextin = cp->nextout = cp->cars_in = cp->cars_out = 0;
  124. cp->capacity = capacity;
  125. /* Initialise our data structure */
  126. cp->carpark = (int *)malloc(cp->capacity * sizeof (*cp->carpark));
  127. /* Check malloc succeeded */
  128.  
  129. if (cp->carpark == NULL) {
  130. perror("malloc()");
  131. exit(EXIT_FAILURE);
  132. }
  133.  
  134. /* Initialise lock and condition variables */
  135. pthread_mutex_init(&cp->lock,NULL);
  136. pthread_cond_init(&cp->space,NULL);
  137. pthread_cond_init(&cp->car,NULL);
  138. /* Seed random number generator */
  139. srand((unsigned int)getpid());
  140.  
  141. return (0);
  142. }
  143.  
  144. int
  145. main(int argc,char *argv[])
  146. {
  147.  
  148. pthread_t p,c,m;
  149. cp_t cp;
  150. /* Check usage */
  151.  
  152. if (argc != 2) {
  153. printf("Usage: %s buffer_size\n",argv[0]);
  154. exit(EXIT_FAILURE);
  155. }
  156.  
  157. /* Initialise */
  158. init(&cp,atoi(argv[1]));
  159. /* Create our threads */
  160. pthread_create(&p,NULL,producer,(void *)&cp);
  161. pthread_create(&p,(void *)&cp);
  162. pthread_create(&c,consumer,(void *)&cp);
  163. pthread_create(&m,monitor,(void *)&cp);
  164. /* Wait for our threads */
  165. pthread_join(p,NULL);
  166. pthread_join(p,NULL);
  167. pthread_join(c,NULL);
  168. pthread_join(m,NULL);
  169.  
  170. return (0);
  171. }

解决方法

我可能会给你完整的答案,但我害怕Lasse V. Karlsen.所以我会给你提示.

>您的struct cp_t中已经可以访问屏障对象栏
>使用pthread_barrier_init初始化它,就像初始化互斥锁一样.计数与演员数量之间存在对应关系.
>在开始生产/消费之前,生产者和消费者都需要wait.得到它 ?

猜你在找的C&C++相关文章