技术标签: 编程语言C
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
第一个参数为需要等待的条件,第二个参数为互斥锁
一般该函数和 int pthread_cond_signal(pthread_cond_t *cond);函数一同使用,用来唤醒在cond条件上等待且处于就绪队列的线程执行。
#include<stdio.h>
#include<sys/types.h>
#include<stdlib.h>
#include<unistd.h>
#include<pthread.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int count = 0;
void *decrement(void *arg) {
printf("in derement.\n");
pthread_mutex_lock(&mutex);
if (count == 0)
pthread_cond_wait(&cond, &mutex);
count--;
printf("----decrement:%d.\n", count);
printf("out decrement.\n");
pthread_mutex_unlock(&mutex);
return NULL;
}
void *increment(void *arg) {
printf("in increment.\n");
pthread_mutex_lock(&mutex);
count++;
printf("----increment:%d.\n", count);
if (count != 0)
pthread_cond_signal(&cond);
printf("out increment.\n");
pthread_mutex_unlock(&mutex);
return NULL;
}
int main(int argc, char *argv[]) {
pthread_t tid_in, tid_de;
pthread_create(&tid_de, NULL, (void*)decrement, NULL);
sleep(2);
pthread_create(&tid_in, NULL, (void*)increment, NULL);
sleep(5);
pthread_join(tid_de, NULL);
pthread_join(tid_in, NULL);
pthread_mutex_destroy(&mutex);
pthread_cond_destroy(&cond);
return 0;
}
我们能够发现decrement使用pthread_cond_wait之前需要先对互斥变量加锁,同时将互斥变量做为参数传入到该函数之中,为什么要做这样的锁操作呢?
这里pthread_cond_wait() 的作用非常重要 – 它是 POSIX 线程信号发送系统的核心
关于该过程在《unix高级环境编程》中的描述如下:
The mutex passed to pthread_cond_wait protects the condition.The caller passes it locked to the function, which then atomically places them calling thread on the list of threads waiting for the condition and unlocks the mutex. This closes the window between the time that the condition is checked and the time that the thread goes to sleep waiting for the condition to change, so that the thread doesn’t miss a change in the condition. When pthread_cond_wait returns, the mutex is again locked
大体意思如下:
pthread_cond_wait函数传入的参数mutex用于保护条件,因为我们在调用pthread_cond_wait时,如果条件不成立我们就进入阻塞,但是进入阻塞这个期间,如果条件变量改变了的话(假如多个唤醒信号所处线程并行执行),那我们就漏掉了这个条件。但此时这个线程还没有放到等待队列上,所以就无法执行phtread_cond_wait所处线程。
解决办法即调用pthread_cond_wait前要先锁互斥量,即调用pthread_mutex_lock(),pthread_cond_wait在把线程放进阻塞队列后,自动对mutex进行解锁,使得其它线程可以获得加锁的权利。这样其它线程才能对临界资源进行访问并在适当的时候唤醒这个阻塞的进程,当pthread_cond_wait返回的时候又自动给mutex加锁。
所以以上代码中pthread_cond_wait和pthread_cond_signal配对使用过程如下
pthread_cond_wait前要先加锁
pthread_cond_wait内部会解锁,然后等待条件变量被其它线程**
pthread_cond_wait被**后会再自动加锁
**线程:
加锁(和等待线程用同一个锁)
pthread_cond_signal发送信号
解锁
**线程的上面三个操作在运行时间上都在等待线程的pthread_cond_wait函数内部
pthread_cond_wait总和一个互斥锁结合使用。在调用pthread_cond_wait前要先获取锁。pthread_cond_wait函数执行时先自动释放指定的锁,然后等待条件变量的变化。在函数调用返回之前,自动将指定的互斥量重新锁住。 int pthread_cond_signal(pthread_cond_t * cond); pthread_cond_signal通过条件变量co...
通常,和pthread _cond_wait 配对使用的有pthread_cond_signal , 同时还有用于pthread_cond_t初始化的pthread_cond_init,销毁的pthread_cond_destroy函数,还有用于加锁保护的pthread_mutex_lock和pthread_mutex_unlock,稍后会对为什么进行加锁做解释。 &nbs...
使用pthread_cond_wait方式如下: pthread _mutex_lock(&mutex) while或if(线程执行的条件是否成立) &...
编译和运行: 测试代码:pthread_cond.c 运行结果:...
一、什么是条件变量 与互斥锁不同,条件变量是用来等待而不是用来上锁的。条件变量用来自动阻塞一个线程,直到某特殊情况发生为止。通常条件变量和互斥锁同时使用。 条件变量使我们可以睡眠等待某种条件出现。条件变量是利用线程间共享的全局变量进...
作者:猫已经找不回了 来源:CSDN 原文:https://blog.csdn.net/hairetz/article/details/4535920 版权声明:本文为博主原创文章,转载请附上博文链接! --------------------------------------------------------------------------------...
关于多线程下条件变量的作用这里就不多讲解了,这里主要是针对条件变量操作时需要注意一个特性虚假唤醒,首先看一段代码。 上述代码中可以发现在pthread_cond_wait上层有一层队列循环检测,目的就是为了处理虚假唤醒,因为我们用条件变量的时候等待cond信号,这个时候可能因为某些特殊条件,我们的cond_wait会被意外唤醒,所以我们需要再次对资源进行检测,确保是否达到所有触发的条件,如果没有满...
条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作: 1: 一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。 2: 为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。 pthread_cond_wait 常用于两个不同线程(主线程与子线程)之间的同步操作。配合lock锁来使用 pthread_...
使用流程 等待线程: pthread_mutex_lock(&mutex); if(条件不满足) pthread_cond_wait(&cond, &mutex); //处理共享资源 pthread_mutex_unlock(&mutex); **线程: pthread_mutex_lock(&mutex); pthread_cond_si...
1.先了解一下等待队列。(默认大家了解mutex,如果不了解:https://blog.csdn.net/qq_33890670/article/details/79967231) 等待队列,是指linux系统中进程所组成的队列,就是需要其他事件的发生才会自己本身被唤醒的进程,也就是说这些进程本身是在等待其他某些进程为他 们提供进程发生的条件。他们是属于消费者的,但是现在他们要消耗的东西还没有产生...