Chinaunix首页 | 论坛 | 博客
  • 博客访问: 47370
  • 博文数量: 9
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 110
  • 用 户 组: 普通用户
  • 注册时间: 2012-07-24 12:19
文章分类

全部博文(9)

文章存档

2017年(4)

2016年(5)

我的朋友

分类: LINUX

2016-10-28 09:43:47

The pthread_cond_wait() and pthread_cond_timedwait() functions are used to block on a condition variable. They are called with mutex locked by the calling thread or undefined behaviour will result.

These functions atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to or in that thread behaves as if it were issued after the about-to-block thread has blocked.

文字解释如下:

pthread_mutex_lock: 获得锁则锁定然后执行第二句话,否则阻塞等锁。
pthread_cond_wait :1.首先解锁相当于pthread_mutex_unlock。2.然后建立锁与条件变量的联系,3.等待唤醒,4.唤醒后第一件事情是上锁相当于pthread_mutex_lock,然后执行下一句话。
生产者--消费者编程模型

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <pthread.h>

  5. #define BUFF_SIZE        8

  6. typedef struct {
  7.     int buff[BUFF_SIZE];
  8.     pthread_mutex_t locker;
  9.     pthread_cond_t notEmpty;
  10.     pthread_cond_t notFull;
  11.     int posReadFrom, posWriteTo;
  12. } Products;

  13. int buffer_is_full(Products* product) {
  14.     if ((product->posWriteTo + 1) % BUFF_SIZE == product->posReadFrom) return 1;
  15.     return 0;
  16. }

  17. int buffer_is_empty(Products* product) {
  18.     if (product->posReadFrom == product->posWriteTo) return 1;
  19.     return 0;
  20. }

  21. void produce(Products* product, int item)
  22. {
  23.     pthread_mutex_lock(&product->locker);

  24.     while (buffer_is_full(product)) {
  25.         pthread_cond_wait(&product->notFull, &product->locker);
  26.     }

  27.     product->buff[product->posWriteTo++] = item;
  28.     if (product->posWriteTo >= BUFF_SIZE)
  29.         product->posWriteTo = 0;

  30.     // 发信号--not empty
  31.     pthread_cond_signal(&product->notEmpty);
  32.     pthread_mutex_unlock(&product->locker);
  33. }

  34. int consume(Products* product)
  35. {
  36.     int item;
  37.     pthread_mutex_lock(&product->locker);

  38.     while (buffer_is_empty(product)) {
  39.         pthread_cond_wait(&product->notEmpty, &product->locker);
  40.     }

  41.     item = product->buff[product->posReadFrom++];
  42.     if (product->posReadFrom >= BUFF_SIZE)
  43.         product->posReadFrom = 0;

  44.     // 发信号--not full
  45.     pthread_cond_signal(&product->notFull);
  46.     pthread_mutex_unlock(&product->locker);
  47.     
  48.     return item;
  49. }



  50. #define END_FLAG    -1
  51. Products gproduce;

  52. void* producer_thread(void* arg)
  53. {
  54.     for (int i = 0; i < 16; i++) {
  55.         printf("producer:%d\n", i);
  56.         produce(&gproduce, i);
  57.         usleep(1000*100);
  58.     }

  59.     produce(&gproduce, END_FLAG);
  60.     return NULL;
  61. }

  62. void* consumer_thread(void* arg)
  63. {
  64.     while (1) {
  65.         int item = consume(&gproduce);
  66.         if (END_FLAG == item) break;
  67.         printf("consumer:%d\n", item);
  68.     }

  69.     return NULL;
  70. }

  71. int main(int argc, char* argv[])
  72. {
  73.     pthread_t producer_id, consumer_id;
  74.     int result;

  75.     pthread_create(&producer_id, NULL, producer_thread, NULL);
  76.     pthread_create(&consumer_id, NULL, consumer_thread, NULL);

  77.     pthread_join(producer_id, NULL);
  78.     pthread_join(consumer_id, NULL);

  79.     return 0;
  80. }
附代码及编译文件
pthreadtest.rar


阅读(1507) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:BBB搭建实时linux系统--基于Adeos及Xenomai扩展

给主人留下些什么吧!~~