POSIX 条件变量
来源:互联网 发布:20世纪90年代网络繁荣 编辑:程序博客网 时间:2024/05/16 10:19
1.条件变量
(1)当一个线程互斥地访问某个变量时,它可能发现在其它线程改变状态之前,它什么也做不了。例如:一个线程访问队列时,发现队列为空,它只能等待,直到其它线程将节点添加到队列中。这种情况就需要用到条件变量。
(2)用于线程间同步共享数据的值。条件变量提供了一种线程间的通知机制:当某个共享数据达到某个值的时候,唤醒等待这个共享数据的线程。
(3)无界缓冲区
(4) 多个消费者可以同时等待这个变量,生产者可以操作这个变量
2.条件变量使用规范
1.等待条件代码
pthread_mutex_lock(&mutex);while (条件为假){ pthread_cond_wait(&cond, &mutex);}修改条件pthread_mutex_unlock(&mutex);
2.给条件发送信号代码
pthread_mutex_lock(&mutex);设置条件为真pthread_cond_signal(&cond);pthread_mutex_unlock(&mutex);
3.条件变量函数
1.pthread_cond_init
(1)功能:初始化条件变量
2.pthread_cond_destory
(1)该函数可以用来摧毁所指定的条件变量,同时将会释放所给它分配的资源。调用该函数的进程并不要求等待在参数所指定的条件变量上;
3.pthread_cond_wait
pthread_cond_wait原语完成三件事:
(1)对mutex解锁;
(2)等待条件, 直到有线程向他发送通知;
(3)当wait返回时, 再对mutex重新加锁;
4.pthread_cond_signal
(1)pthread_cond_signal原语所完成的操作:
向第一个等待条件的线程发起通知, 如果没有任何一个线程处于等待条件的状态, 那么这个通知将被忽略;
(2)pthread_cond_broadcast:
向所有等待在该条件上的线程发送通知;
4.使用条件变量解决生产者消费者问题
#include <unistd.h>#include <sys/types.h>#include <sys/ipc.h>#include <sys/msg.h>#include <sys/stat.h>#include <fcntl.h>#include <sys/shm.h>#include <pthread.h>#include <semaphore.h>#include <stdlib.h>#include <stdio.h>#include <errno.h>#include <string.h>#define ERR_EXIT(m) \ do \ { \ perror(m); \ exit(EXIT_FAILURE); \ }while(0)#define CONSUMERS_COUNT 2#define PRODUCERS_COUNT 5pthread_mutex_t g_mutex;pthread_cond_t g_cond;pthread_t g_thread[CONSUMERS_COUNT + PRODUCERS_COUNT];//无界缓冲区int nready = 0;//表示当前缓冲区产品个数void* consume(void* arg) {int i;int num = *(int*)arg;free(arg);while(1) {pthread_mutex_lock(&g_mutex);while(nready == 0) {printf("%d begin wait a condition ...\n", num);pthread_cond_wait(&g_cond, &g_mutex);}printf("%d end wait a condition ...\n", num);printf("%d begin consume product ...\n", num);--nready; // 消费产品printf("%d end consume product ...\n", num);pthread_mutex_unlock(&g_mutex);sleep(5);}return NULL;}void* produce(void* arg) {int i;int num = *(int*)arg;free(arg);while(1) {pthread_mutex_lock(&g_mutex);printf("%d begin produce product ...\n", num);++nready;printf("%d end produce product ...\n", num);pthread_cond_signal(&g_cond);printf("%d signal ...\n", num);pthread_mutex_unlock(&g_mutex);sleep(5);}return NULL;}int main(int argc, char* argv[]) {int i;pthread_mutex_init(&g_mutex, NULL); pthread_cond_init(&g_cond, NULL);for(i=0; i<CONSUMERS_COUNT; i++) {int* p = (int*)malloc(sizeof(int));*p = i;pthread_create(&g_thread[i], NULL, consume, (void*)p); }sleep(1);for(i=0; i<PRODUCERS_COUNT; i++) {int* p = (int*)malloc(sizeof(int));*p = i;pthread_create(&g_thread[CONSUMERS_COUNT+i], NULL, produce, (void*)p); }for(i=0; i<CONSUMERS_COUNT+PRODUCERS_COUNT; i++) {pthread_join(g_thread[i], NULL);}//pthread_mutex_destory(&g_mutex);//pthread_cond_destory(&g_cond);return 0;}
阅读全文
1 0
- POSIX线程:条件变量
- posix 条件变量
- Posix多线程-条件变量
- POSIX线程:条件变量
- POSIX多线程--条件变量
- Posix条件变量
- POSIX条件变量
- POSIX多线程--条件变量
- POSIX条件变量
- POSIX条件变量
- POSIX 条件变量
- POSIX 条件变量
- POSIX 条件变量详细解析
- POSIX 条件变量详细解析
- 正确使用POSIX条件变量
- posix 多线程程序使用条件变量
- linux Posix线程同步(条件变量) 实例
- posix多线程编程之条件变量
- yii2实用插件总结
- Ubuntu下安装并配置maven
- 1. Steps to set up java web project with IDEA
- 第二次MySQL与Jsp学习笔记
- com.android.volley.toolbox.DiskBasedCache.streamToBytes异常
- POSIX 条件变量
- Android使用MultiDex处理64K限制
- Linux下crond、crontab命令
- docker容器间网络通信
- 学习笔记:复习单调队列
- linux下的select简直太奇葩了:1024限定的不只是监听的个数,还是文件描述符的最大值,注意,是值
- Java经典算法40例(一)
- 匿名内部类
- spring profile激活处理