POSIX 条件变量

xiaoxiao2021-02-28  96

条件变量:条件变量是用来等待而不是用来上锁的。条件变量用来自动阻塞一个线程,直到某特殊情况发生为止。通常条件变量和互斥锁同时使用。 因此, 当一个线程互斥的访问某个变量时,它可能发现在其它线程改变状态之前,它什么也做不了。例如一个线程访问队列时,发现队列为空时,他只能等待,直到其它线程将一个节点添加到队列中。这种情况就可以用到条件变量。 int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr); int pthread_cond_wait(pthread_cond_t *cond,pthread_mutex_t *mutex); int pthread_cond_timewait(pthread_cond_t *cond,pthread_mutex *mutex,const timespec *abstime); int pthread_cond_destroy(pthread_cond_t *cond); int pthread_cond_signal(pthread_cond_t *cond); int pthread_cond_broadcast(pthread_cond_t *cond); //解除所有线程的阻塞 简要说明: (1)初始化.init()或者pthread_cond_t cond=PTHREAD_COND_INITIALIER;属性置为NULL (2)等待条件成立.pthread_wait,pthread_timewait.wait()释放锁,并阻塞等待条件变量为真 timewait()设置等待时间,仍未signal,返回ETIMEOUT(加锁保证只有一个线程wait) (3)激活条件变量:pthread_cond_signal,pthread_cond_broadcast(激活所有等待线程) (4)清除条件变量:destroy;无线程等待,否则返回EBUSY 条件变量使用规范: 等待条件变量代码: pthread_mutex_lock(&mutex); while (条件为假) pthread_cond_wait(cond, mutex); 修改条件 pthread_mutex_unlock(&mutex);

给条件信号发送信号代码:

pthread_mutex_lock(&mutex); while (条件为真); pthread_cond_signal(cond, mutex); 修改条件 pthread_mutex_unlock(&mutex); 细节问题 pthread_cond_wait(&cond, &mutex); 内部完成了三件事: 1. 对mutex进行解锁;2,等待条件,直到有线程向它发起通知;3,重新对mutex进行加锁。 pthread_cond_signal(&cond); 向第一个等待条件的线程发起通知,如果没有任何一个线程处于等待条件的状态,这个通知将被忽略。

为什么检测条件用while而不是if?因为pthread_cond_wait会产生信号,有两种情况,一种是pthread_cond_wait会自动重启,好像这个信号没有发生一样,第二种pthread_cond_wait可能会被虚假唤醒,因此还需要重新判断。

#include<pthread.h> #include<unistd.h> #include<stdio.h> #include<sys/types.h> #include<stdlib.h> #include<errno.h> #include<string.h> #include<semaphore.h> #include <fcntl.h> /* For O_* constants */ #include <sys/stat.h> #define ERR_EXIT(m) do{perror(m);exit(EXIT_FAILURE);}while(0) #define CONSUMER_COUNT 2 #define PRODUCER_COUNT 1 pthread_cond_t g_cond; pthread_mutex_t g_mutex; pthread_t g_thread[CONSUMER_COUNT+PRODUCER_COUNT]; int nready=0;// void* consume(void *arg) { int n=(int)((long)arg); while(1) { pthread_mutex_lock(&g_mutex); while(nready==0) { printf("%d consuer begin wait\n",n); pthread_cond_wait(&g_cond,&g_mutex); } printf("%d end wait a condtion...\n",n); printf("%d begin consume\n",n); --nready; printf("%d end consume\n",n); pthread_mutex_unlock(&g_mutex); sleep(1); } return NULL; } void* produce(void *arg) { int n=(int)((long)arg); while(1) { pthread_mutex_lock(&g_mutex); printf("%d begin produce\n",n); ++nready; printf("%d end produce\n",n); printf("%d signal ....\n",n); pthread_cond_signal(&g_cond); pthread_mutex_unlock(&g_mutex); sleep(5); } return NULL; } int main() { pthread_cond_init(&g_cond,NULL); pthread_mutex_init(&g_mutex,NULL); int i=0; for(i=0;i<CONSUMER_COUNT;i++) { pthread_create(&g_thread[i],NULL,consume,(void*)((long) i)); } sleep(2); for(i=0;i<PRODUCER_COUNT;i++) { pthread_create(&g_thread[i+CONSUMER_COUNT],NULL,produce,(void*)((long) i)); } for(i=0;i<CONSUMER_COUNT+PRODUCER_COUNT;i++)pthread_join(g_thread[i],NULL); pthread_mutex_destroy(&g_mutex); pthread_cond_destroy(&g_cond); }

转载请注明原文地址: https://www.6miu.com/read-67178.html

最新回复(0)