pthread cond 生产者消费者(复习)
来源:互联网 发布:电脑一般装什么软件 编辑:程序博客网 时间:2024/06/03 22:41
#include <stdio.h>#include <stdlib.h>#include <unistd.h>#include <pthread.h>#define BUFFER_SIZE 8struct products{ int buffer[BUFFER_SIZE]; pthread_mutex_t lock; pthread_cond_t not_empty; pthread_cond_t not_full; int pos_read_from; int pos_write_to;};int buffer_is_full(struct products *products){ if ((products->pos_write_to + 1) % BUFFER_SIZE == products->pos_read_from) { return 1; } else { return 0; }}int buffer_is_empty(struct products *products){ if (products->pos_write_to == products->pos_read_from) { return 1; } else { return 0; }}void produce(struct products *products, int item){ pthread_mutex_lock(&products->lock); while (buffer_is_full(products)) { pthread_cond_wait(&products->not_full, &products->lock); } products->buffer[products->pos_write_to] = item; printf("pos write to:%d\n", products->pos_write_to); products->pos_write_to++; if (products->pos_write_to >= BUFFER_SIZE) { products->pos_write_to = 0; } pthread_cond_signal(&products->not_empty); pthread_mutex_unlock(&products->lock);}int consume(struct products *products){ int item; pthread_mutex_lock(&products->lock); while (buffer_is_empty(products)) { pthread_cond_wait(&products->not_empty, &products->lock); } item = products->buffer[products->pos_read_from]; printf("pos write to:%d\n", products->pos_read_from); products->pos_read_from++; if (products->pos_read_from >= BUFFER_SIZE) { products->pos_read_from = 0; } pthread_cond_signal(&products->not_full); //because of context switch pthread_mutex_unlock(&products->lock); return item;}#define END_FLAG (-1)struct products products;void * produce_thread(void *data){ int i; for (i=0; i<16; i++) { printf("produce %d\n", i); produce(&products, i); } produce(&products, END_FLAG); return NULL;}void * consume_thread(void *data){ int item; while (1) { item = consume(&products); if (END_FLAG == item) { break; } printf("consumer:%d\n", item); } return NULL;}int init_products(){ if (pthread_mutex_init(&products.lock, NULL) != 0) { perror("mutex init failed"); exit(-1); } if (pthread_cond_init(&products.not_full, NULL) != 0) { perror("cond not full init failed"); exit (-1); } if (pthread_cond_init(&products.not_empty, NULL) != 0) { perror("cond not empty init failed"); exit (-1); } products.pos_read_from = 0; products.pos_write_to = 0;}int main(int argc, char *argv[]){ int res; pthread_t producer, consumer; pthread_create(&producer, NULL, &produce_thread, NULL); pthread_create(&consumer, NULL, &consume_thread, NULL); pthread_join(producer, (void *)&res); pthread_join(consumer, (void *)&res); return 0;}
0 0
- pthread cond 生产者消费者(复习)
- pthread 生产者消费者
- pthread-生产者-消费者问题
- pthread-消费者/生产者模型实现
- pthread 条件变量(cond),sem,mutex
- 最简单的生产者消费者-pthread
- 复杂一点的生产者消费者-pthread
- 生产者消费者问题的pthread模拟
- 线程的生产者消费者复习
- 考研复习(11)-生产者消费者问题
- Java基础复习:线程通信、生产者消费者
- 复习之生产者——消费者问题
- 【java基础】多线程--生产者消费者--复习
- Java复习之生产者消费者案例
- 【复习笔记】手写生产者消费者 & 死锁
- pthread-生产者消费者问题【采用互斥量和条件变量】
- [C++]_[初级]_[pthread多线程之生产者消费者模型]
- 黑马程序员--JAVA基础复习之多线程(三)线程间通信 生产者消费者
- 读书笔记_Effective C++_习惯C++
- The specified virtual disk needs repair.
- php取整函数
- 妈妈教女程序猿如何找到好男人
- iframe自适应高度的多种方法方法小结
- pthread cond 生产者消费者(复习)
- 运营商数据治理实践
- easyui简单登陆界面
- Form验证提交
- java中的main()方法
- VB老鸟初学Python(Python for kids)
- C++内存分配
- java 访问控制
- 数据加密中的DES加密算法详解