线程同步之 pthread_cond_wait 和pthread_mutex
来源:互联网 发布:淘宝主类目怎么修改 编辑:程序博客网 时间:2024/06/06 07:11
pthread_cond_wait 和pthread_mutex常用于父子线程之间的消息通知。
1、条件变量 pthread_cond_t
条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
(1) 创建和注销
条件变量和互斥锁一样,都有静态动态两种创建方式,静态方式使用PTHREAD_COND_INITIALIZER常量,如下:
pthread_cond_t cond=PTHREAD_COND_INITIALIZER
动态方式调用pthread_cond_init()函数,API定义如下:
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)
尽管POSIX标准中为条件变量定义了属性,但在LinuxThreads中没有实现,因此cond_attr值通常为NULL,且被忽略。
注销一个条件变量需要调用pthread_cond_destroy(),只有在没有线程在该条件变量上等待的时候才能注销这个条件变量,否则返回EBUSY。因为Linux实现的条件变量没有分配什么资源,所以注销动作只包括检查是否有等待线程。API定义如下:
int pthread_cond_destroy(pthread_cond_t *cond)
(2) 等待和激发
int pthread_cond_wait(pthread_cond_t *cond, pthread_mutex_t *mutex)
int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime)
等待条件有两种方式:无条件等待pthread_cond_wait()和计时等待pthread_cond_timedwait(),其中计时等待方式如果在给定时刻前条件没有满足,则返回ETIMEOUT,结束等待,其中abstime以与time()系统调用相同意义的绝对时间形式出现,0表示格林尼治时间1970年1月1日0时0分0秒。
无论哪种等待方式,都必须和一个互斥锁配合,以防止多个线程同时请求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()前的加锁动作对应。
激发条件有两种形式,pthread_cond_signal()激活一个等待该条件的线程,存在多个等待线程时先按线程优先级,然后按入队顺序激活其中一个;而pthread_cond_broadcast()则激活所有等待线程。
给出一个完整例子:
#include <pthread.h>#include <unistd.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;/*[thread_func]*/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) { pthread_mutex_lock(&mtx); //这个mutex主要是用来保证pthread_cond_wait的并发性 while (head == NULL) { //这个while要特别说明一下,单个pthread_cond_wait功能很完善,为何这里要有一个while (head == NULL)呢?因为pthread_cond_wait里的线程可能会被意外唤醒,如果这个时候head != NULL,则不是我们想要的情况。这个时候,应该让线程继续进入pthread_cond_wait pthread_cond_wait(&cond, &mtx); // pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mtx,然后阻塞在等待对列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mtx);,再读取资源 //用这个流程是比较清楚的/*block-->unlock-->wait() return-->lock*/ } 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); //子线程会一直等待资源,类似生产者和消费者,但是这里的消费者可以是多个消费者,而不仅仅支持普通的单个消费者,这个模型虽然简单,但是很强大 /*[tx6-main]*/ for (i = 0; i < 10; i++) { p = 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(tid); //关于pthread_cancel,有一点额外的说明,它是从外部终止子线程,子线程会在最近的取消点,退出线程,而在我们的代码里,最近的取消点肯定就是pthread_cond_wait()了。关于取消点的信息,有兴趣可以google,这里不多说了 pthread_join(tid, NULL); printf("All done -- exiting/n"); return 0;}
2、pthread_cond_wait()与pthread_mutex 配套使用的问题
pthread_cond_wait()函数一进入wait状态就会自动release mutex
pthread_cond_wait() 一旦wait成功获得cond 条件的时候会自动 lock mutex.
(1)pthread_cond_wait之前加锁原因
In Thread1:
pthread_mutex_lock(&m_mutex);
pthread_cond_wait(&m_cond,&m_mutex);
pthread_mutex_unlock(&m_mutex);
In Thread2:
pthread_mutex_lock(&m_mutex);
pthread_cond_signal(&m_cond);
pthread_mutex_unlock(&m_mutex);
为什么要与pthread_mutex 一起使用呢? 这是为了应对线程1在调用pthread_cond_wait()但线程1还没有进入wait cond的状态的时候,此时线程2调用了 cond_singal 的情况。 如果不用mutex锁的话,这个cond_singal就丢失了。
加了锁的情况是,线程2必须等到 mutex 被释放(也就是 pthread_cond_wait() 进入wait_cond状态 并自动释放mutex)
的时候才能调用cond_singal.
(2)pthread_cond_wait之后解锁原因
pthread_cond_wait() 一旦wait成功获得cond 条件的时候会自动 lock mutex.我对这里返回加锁的理解是线程1需要读取资源(也可能在用户区操作临界资源),需要让线程2执行完发信号并解锁后pthread_cond_wait才能返回,从编程上看也与之前的加锁状态相对应。
(3)取消点问题
这就会出现另一个问题。这是因为
The pthread_cond_wait() and pthread_cond_timedwait() is a cancellation point.
In Thread3:
pthread_cancel(&m_thread);
pthread_join();
因为pthread_cond_wait() and pthread_cond_timedwait() 是线程退出点函数,因此在Thread3中
可以调用pthread_cancel()来退出线程1。那样显然线程1会在 pthread_cond_wait(&m_cond,&m_mutex);
和 pthread_mutex_unlock(&m_mutex); 之间退出, pthread_cond_wait() 函数返回后自动lock住了mutex,
这个时候线程1退出(并没有运行到pthread_mutex_unlock()),如果Thread2这个时候就再也得不到lock状态了。
通常解决这个问题的办法如下
void cleanup(void *arg)
{
pthread_mutex_unlock(&mutex);
}
void * thread1(void * arg)
{
pthread_cleanup_push(cleanup, NULL); // thread cleanup handler
pthread_mutex_lock(&mutex);
pthread_cond_wait(&cond, &mutex);
pthread_mutex_unlock(&mutex);
pthread_cleanup_pop(0 );
}
(4)pthread_cond_signal 是否需要加锁
在不关心调度顺序的情况下是可以不需要加锁的。这样可以避免thread2 在使用m_mutex 时发现thread1 正在使用而转入睡眠状态,提高了效率。
使用例子如下
In Thread1:
pthread_mutex_lock(&m_mutex);
pthread_cond_wait(&m_cond,&m_mutex);
pthread_mutex_unlock(&m_mutex);
In Thread2:
pthread_cond_signal(&m_cond);
在这种情况下,如果m_mutex 只是在条件变量中使用则不需要 pthread_cleanup_push 和 pthread_cleanup_pop 了。
详细可参考:http://blog.csdn.net/chenjiayi_yun/article/details/37517485
- 线程同步之 pthread_cond_wait 和pthread_mutex
- 线程同步之条件变量:pthread_cond_signal和pthread_cond_wait
- 线程同步之条件变量:pthread_cond_signal和pthread_cond_wait
- 线程互斥和条件同步,pthread_mutex和pthread_cond
- [Linux]多线程同步之pthread_cond_wait()学习笔记
- pthread_mutex的任务间同步
- 线程(6)--线程同步之同步方法和同步块
- pthread_mutex
- pthread_cond_wait多线程同步
- 线程之线程同步
- 线程同步之mutex和条件变量
- 面试问题之线程同步和死锁
- Java多线程之线程同步和死锁
- 线程和线程同步
- 线程和线程同步
- 线程和线程同步
- java线程安全之同步函数和同步代码块
- 线程通信和线程同步【操作系统之哲学原理读书笔记】
- hibernate4整合spring3出现java.lang.NoClassDefFoundError: [Lorg/hibernate/engine/FilterDefinition;
- 实习生活第一篇,Eclipse_andriod系统配置
- 安装BO4.1 出现 重新启动挂起
- JSP监听器的使用
- OC重点API
- 线程同步之 pthread_cond_wait 和pthread_mutex
- VC如何自定义消息
- CSS之Position详解
- 数据库的增删改查和使用流程
- SQL运算符
- vim显示行号、语法高亮、自动缩进的设置
- JSP协议之HTTP协议
- lua写的UI复用模块代码
- IOS7 UUID的使用和对比