条件变量
条件变量是利用线程间共享全局变量进行同步的一种机制。
条件变量上的基本操作有:
- 触发条件(当条件变为 true 时);
- 等待条件,挂起线程直到其他线程触发条件。
声明
//初始化条件变量
//尽管POSIX标准中为条件变量定义了属性,但在Linux中没有实现,因此cond_attr值通常为NULL,且被忽略。
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);
/*
无论哪种等待方式,都必须和一个互斥锁配合,以防止多个线程同时请求(用 pthread_cond_wait() 或 pthread_cond_timedwait() 请求)竞争条件(Race Condition)。
mutex互斥锁必须是普通锁(PTHREAD_MUTEX_TIMED_NP)或者适应锁(PTHREAD_MUTEX_ADAPTIVE_NP),
且在调用pthread_cond_wait()前必须由本线程加锁(pthread_mutex_lock()),
而在更新条件等待队列以前,mutex保持锁定状态,并在线程挂起进入等待前解锁。
在条件满足从而离开pthread_cond_wait()之前,mutex将被重新加锁,以与进入pthread_cond_wait()前的加锁动作对应。
*/
//激活一个等待该条件的线程(存在多个等待线程时按入队顺序激活其中一个
int pthread_cond_signal(pthread_cond_t *cond);
//激活所有等待线程
int pthread_cond_broadcast(pthread_cond_t *cond);
//销毁条件变量
int pthread_cond_destroy(pthread_cond_t *cond);
说明:
- pthread_cond_wait
- 自动解锁互斥量(如同执行了pthread_unlock_mutex),并等待条件变量触发。
- 这时线程挂起,不占用CPU时间,直到条件变量被触发(变量为ture)。
- 在调用 pthread_cond_wait之前,应用程序必须加锁互斥量。
- pthread_cond_wait函数返回前,自动重新对互斥量加锁(如同执行了pthread_lock_mutex)。
- 就是说 使用这个函数会自动解锁 ,在满足条件的时候自动加锁
- 互斥量的解锁和在条件变量上挂起都是自动进行的。
- 因此,在条件变量被触发前,如果所有的线程都要对互斥量加锁,这种机制可保证在线程加锁互斥量和进入等待条件变量期间,条件变量不被触发。
- 条件变量要和互斥量相联结,以避免出现条件竞争——个线程预备等待一个条件变量,当它在真正进入等待之前,另一个线程恰好触发了该条件(条件满足信号有可能在测试条件和调用pthread_cond_wait函数(block)之间被发出,从而造成无限制的等待)。
- 条件变量函数不是异步信号安全的,不应当在信号处理程序中进行调用。
- 特别要注意,如果在信号处理程序中调用 pthread_cond_signal 或 pthread_cond_boardcast 函数,可能导致调用线程死锁
例子
#include <pthread.h>
#include <unistd.h>
#include "stdio.h"
#include "stdlib.h"
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
struct node
{
int n_number;
struct node *n_next;
}*head = NULL;
static void cleanup_handler(void *arg)
{
printf("Cleanup handler of second thread.\n");
free(arg);
(void)pthread_mutex_unlock(&mtx);
}
static void *thread_func(void *arg)
{
struct node *p = NULL;
pthread_cleanup_push(cleanup_handler, p);
while (1) {
// 这个mutex主要是用来保证pthread_cond_wait的并发性。
pthread_mutex_lock(&mtx);
while (head == NULL) {
/* 这个while要特别说明一下,单个pthread_cond_wait功能很完善,为何
* 这里要有一个while (head == NULL)呢?因为pthread_cond_wait里的线
* 程可能会被意外唤醒,如果这个时候head != NULL,则不是我们想要的情况。
* 这个时候,应该让线程继续进入pthread_cond_wait
* pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,
* 然后阻塞在等待对列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立
* 而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx);,再读取资源
* 用这个流程是比较清楚的。*/
pthread_cond_wait(&cond, &mtx);
p = head;
head = head->n_next;
printf("Got %d from front of queue\n", p->n_number);
free(p);
}
pthread_mutex_unlock(&mtx); // 临界区数据操作完毕,释放互斥锁。
}
pthread_cleanup_pop(0);
return 0;
}
int main(void)
{
pthread_t tid;
int i;
struct node *p;
/* 子线程会一直等待资源,类似生产者和消费者,但是这里的消费者可以是多个消费者,
* 而不仅仅支持普通的单个消费者,这个模型虽然简单,但是很强大。*/
pthread_create(&tid, NULL, thread_func, NULL);
sleep(1);
for (i = 0; i < 10; i++)
{
p = (struct node*)malloc(sizeof(struct node));
p->n_number = i;
pthread_mutex_lock(&mtx); // 需要操作head这个临界资源,先加锁。
p->n_next = head;
head = p;
pthread_cond_signal(&cond);
pthread_mutex_unlock(&mtx); //解锁
sleep(1);
}
printf("thread 1 wanna end the line.So cancel thread 2.\n");
/* 关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,子线程会在最近的取消点,
* 退出线程,而在我们的代码里,最近的取消点肯定就是pthread_cond_wait()了。*/
pthread_cancel(tid);
pthread_join(tid, NULL);
printf("All done -- exiting\n");
return 0;
}