pthread-生产消费.c -----注释
来源:互联网 发布:linux竖线怎么打 编辑:程序博客网 时间:2024/05/17 22:03
点击(此处)折叠或打开
- /************************************************
- *
- * The classic producer-consumer example.
- * Illustrates mutexes and conditions.
- * by Zou jian guo <ah_zou@tom.com>
- * 2003-12-22
- *
- *************************************************/
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
- #include "pthread.h"
- #define BUFFER_SIZE 16
- /* Circular buffer of integers. */
- struct prodcons {
- int buffer[BUFFER_SIZE]; /* the actual data */
- pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
- int readpos, writepos; /* positions for reading and writing */
- pthread_cond_t notempty; /* signaled when buffer is not empty */
- pthread_cond_t notfull; /* signaled when buffer is not full */
- };
- /*--------------------------------------------------------*/
- /* Initialize a buffer */
- void init(struct prodcons * b)
- {
- pthread_mutex_init(&b->lock, NULL);/*初始化互斥锁*/
- pthread_cond_init(&b->notempty, NULL);/*初始化条件变量notempty*/
- pthread_cond_init(&b->notfull, NULL);/*初始化条件变量notfull*/
- b->readpos = 0;/*初始化读位置*/
- b->writepos = 0;/*初始化写位置*/
- }
- /*--------------------------------------------------------*/
- /* Store an integer in the buffer */
- void put(struct prodcons * b, int data)/*生产*/
- {
- pthread_mutex_lock(&b->lock); /*加锁*/
- /* Wait until buffer is not full */ /*有一个空位就相当于满*/
- while ((b->writepos + 1) % BUFFER_SIZE == b->readpos) { /*判断读写位置是否相等;+1防止两个进程都阻塞*/
- printf("wait for not full\n");
- pthread_cond_wait(&b->notfull, &b->lock);/*解lock互斥锁并阻塞,等待消费者notfull来解除阻塞,
- 被唤醒时再加lock锁*/
- }
- /* Write the data and advance write pointer */
- b->buffer[b->writepos] = data; /*写入数据(产品放入共享区域)*/
- b->writepos++; /*写位置+1 */
- if (b->writepos >= BUFFER_SIZE) b->writepos = 0;
- /* Signal that the buffer is now not empty */
- pthread_cond_signal(&b->notempty); /*释放阻塞在notempty条件变量上的(消费者)线程*/
- pthread_mutex_unlock(&b->lock); /*解锁*/
- }
- /*--------------------------------------------------------*/
- /* Read and remove an integer from the buffer */
- int get(struct prodcons * b)/*消费*/
- {
- int data;
- pthread_mutex_lock(&b->lock);/*加锁*/
- /* Wait until buffer is not empty */
- while (b->writepos == b->readpos) { /*判断读写位置是否相等*/
- printf("wait for not empty\n");
- pthread_cond_wait(&b->notempty, &b->lock);/*解lock互斥锁并阻塞,等待消费者notempty来解除阻塞
- 被唤醒时,再加lock锁*/
- }
- /* Read the data and advance read pointer */
- data = b->buffer[b->readpos]; /*读数据(消费产品)*/
- b->readpos++; /*读位置+1*/
- if (b->readpos >= BUFFER_SIZE) b->readpos = 0;
- /* Signal that the buffer is now not full */
- pthread_cond_signal(&b->notfull); /*释放阻塞在notfull条件变量上的(生产者)线程*/
- pthread_mutex_unlock(&b->lock); /*解锁*/
- return data;
- }
- /*--------------------------------------------------------*/
- #define OVER (-1)
- struct prodcons buffer;
- /*--------------------------------------------------------*/
- void * producer(void * data)/*生产者函数*/
- {
- int n;
- for (n = 0; n < 1000; n++) {/*总产量为1000,生产1000个产品,停止生产*/
- printf(" put-->%d\n", n);
- put(&buffer, n);/*生产产品,放入共享区域*/
- }
- put(&buffer, OVER);/*生产结束标志*/
- printf("producer stopped!\n");
- return NULL;
- }
- /*--------------------------------------------------------*/
- void * consumer(void * data)/*消费者函数*/
- {
- int d;
- while (1) {
- d = get(&buffer);/*消费产品*/
- if (d == OVER ) break;
- printf(" %d-->get\n", d);
- }
- printf("consumer stopped!\n");
- return NULL;
- }
- /*--------------------------------------------------------*/
- int main(void)
- {
- pthread_t th_a, th_b;
- void * retval;
- init(&buffer);/*初始化共享区域*/
- pthread_create(&th_a, NULL, producer, 0);/*创建生产者线程*/
- pthread_create(&th_b, NULL, consumer, 0);/*创建消费者线程*/
- /* Wait until producer and consumer finish. */
- pthread_join(th_a, &retval);/*主线程等该生产者线程*/
- pthread_join(th_b, &retval);/*主线程等该消费者线程*/
- return 0;
- }
0
上一篇:+-小数四舍五入c代码---
下一篇:Linux stat函数讲解
相关热门文章
- test123
- 编写安全代码——小心有符号数...
- 彻底搞定C语言指针详解-完整版...
- 使用openssl api进行加密解密...
- 一段自己打印自己的c程序...
- linux dhcp peizhi roc
- 关于Unix文件的软链接
- 求教这个命令什么意思,我是新...
- sed -e "/grep/d" 是什么意思...
- 谁能够帮我解决LINUX 2.6 10...
给主人留下些什么吧!~~
评论热议
0 0
- pthread-生产消费.c -----注释
- 生产消费
- 生产消费
- 眼镜生产消费
- 生产和消费
- 继 生产和消费
- 多线程生产与消费
- 多线程生产消费问题
- 线程生产消费模式
- 生产消费模式
- 生产消费拿馒头
- 生产消费模式
- Thead 生产消费
- 生产消费模型demo
- 线程---生产消费问题
- 生产消费设计模式
- 生产消费问题
- java生产消费问题
- /etc/passwd&&/etc/shadow
- 自己编的shell脚本,实现备份文本内容(目的:学习交流)
- qaz718035146 是我,我说错了!!现在有真相,明白了!
- +-小数四舍五入c代码---
- Jdk配置
- pthread-生产消费.c -----注释
- javah -jni org.cocos2dx.cpp.JniTest 错误: 找不到 'org.cocos2dx.cpp.JniTest' 的类文件。
- Linux stat函数讲解
- Date、String、Long三种日期类型之间的相互转换
- Linux_Struct file()结构体详解
- JAVA高级【9.1】《Java核心技术2》RMI详解
- Android Studio 将公共工具库打成jar包和arr包的方法!
- linux各文件夹的作用
- iOS应用架构谈 view层的组织和调用方案
原创粉丝点击
热门IT博客
热门问题
老师的惩罚
人脸识别
我在镇武司摸鱼那些年
重生之率土为王
我在大康的咸鱼生活
盘龙之生命进化
天生仙种
凡人之先天五行
春回大明朝
姑娘不必设防,我是瞎子
东方万里行银卡怎么办
碟调网看不了怎么办
电瓶车锁住了怎么办
小便有灼热感怎么办
针灸拔针时出血怎么办
脑积水抽搐怎么办
肝部有血管瘤怎么办
喉咙紧缩感怎么办
艾灸烟大怎么办
有颈椎病怎么办
脖子拔罐黑紫印怎么办
拔火罐发黑怎么办
拔罐后紫黑怎么办
拔火罐拔出水泡怎么办
拔罐子拔出水泡怎么办
拔罐出水泡怎么办
足三里酸痛怎么办
root不完整怎么办
没有root权限怎么办
面包车动力不足怎么办
发生战争老百姓怎么办
一方不同意离婚怎么办
洗衣机不脱水怎么办
公主病怎么办
乳房大小不一,怎么办
严重弱精症怎么办
重度少精怎么办
老公弱精怎么办
北京少精弱精症怎么办
小金毛太瘦了怎么办
我想瘦怎么办
我想瘦点怎么办
手臂太瘦怎么办
喉咙长息肉怎么办
嗓子长息肉怎么办
乳头没有发育,怎么办
胸部有点痛怎么办
胸部发炎怎么办
胡子稀疏怎么办
熬夜眼睛痛怎么办
胃肠不吸收怎么办