Linux多线程—条件变量
来源:互联网 发布:技术支持 恒久软件 编辑:程序博客网 时间:2024/05/16 19:52
非程序部分转载自:http://www.cnblogs.com/feisky/archive/2010/03/08/1680950.html
条件变量简述
与互斥锁不同,条件变量是用来等待而不是用来上锁的。
条件变量用来自动阻塞一个线程,直到某特殊情况发生为止。
通常条件变量和互斥锁同时使用。
条件变量使我们可以睡眠等待某种条件出现。条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待”条件变量的条件成立”而挂起;另一个线程使”条件成立”(给出条件成立信号)。
条件的检测是在互斥锁的保护下进行的。如果一个条件为假,一个线程自动阻塞,并释放等待状态改变的互斥锁。如果另一个线程改变了条件,它发信号给关联的条件变量,唤醒一个或多个等待它的线程,重新获得互斥锁,重新评价条件。如果两进程共享可读写的内存,条件变量可以被用来实现这两进程间的线程同步。
使用条件变量之前要先进行初始化。可以在单个语句中生成和初始化一个条件变量如:pthread_cond_t my_condition=PTHREAD_COND_INITIALIZER;
(用于进程间线程的通信)。可以利用函数pthread_cond_init动态初始化。
条件变量分为两部分: 条件和变量. 条件本身是由互斥量保护的. 线程在改变条件状态前先要锁住互斥量. 它利用线程间共享的全局变量进行同步的一种机制。
相关的函数
如下:
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`
详细说明
1. 初始化:
条件变量采用的数据类型是pthread_cond_t,
在使用之前必须要进行初始化, 这包括两种方式:
•静态: 可以把常量PTHREAD_COND_INITIALIZER
给静态分配的条件变量.
•动态:pthread_cond_init
函数,是释放动态条件变量的内存空间之前,要用pthread_cond_destroy
对其进行清理.
#include <pthread.h>int pthread_cond_init(pthread_cond_t *restrict cond, pthread_condattr_t *restrict attr);int pthread_cond_destroy(pthread_cond_t *cond);
成功则返回0, 出错则返回错误编号.
当pthread_cond_init
的attr参数为NULL时, 会创建一个默认属性的条件变量; 非默认情况以后讨论.
2. 等待条件:
#include <pthread.h>int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restric mutex);int pthread_cond_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict timeout);
成功则返回0, 出错则返回错误编号.
这两个函数分别是阻塞等待和超时等待.等待条件函数等待条件变为真, 传递给pthread_cond_wait
的互斥量对条件进行保护, 调用者把锁住的互斥量传递给函数. 函数把调用线程放到等待条件的线程列表上, 然后对互斥量解锁, 这两个操作是原子的. 这样便关闭了条件检查和线程进入休眠状态等待条件改变这两个操作之间的时间通道, 这样线程就不会错过条件的任何变化.
当pthread_cond_wait
返回时, 互斥量再次被锁住.
3.通知条件:
#include <pthread.h>int pthread_cond_signal(pthread_cond_t *cond);int pthread_cond_broadcast(pthread_cond_t *cond);
成功则返回0, 出错则返回错误编号.
这两个函数用于通知线程条件已经满足. 调用这两个函数, 也称向线程或条件发送信号. 必须注意, 一定要在改变条件状态以后再给线程发送信号.
条件变量与互斥锁、信号量的区别
1.互斥锁必须总是由给它上锁的线程解锁,信号量的挂出即不必由执行过它的等待操作的同一进程执行。一个线程可以等待某个给定信号灯,而另一个线程可以挂出该信号灯。 2.互斥锁要么锁住,要么被解开(二值状态,类型二值信号量)。 3.由于信号量有一个与之关联的状态(它的计数值),信号量挂出操作总是被记住。然而当向一个条件变量发送信号时,如果没有线程等待在该条件变量上,那么该信号将丢失。 4.互斥锁是为了上锁而设计的,条件变量是为了等待而设计的,信号灯即可用于上锁,也可用于等待,因而可能导致更多的开销和更高的复杂性。
简单程序实例
#include <stdio.h>#include <pthread.h>#include <stdlib.h>pthread_mutex_t m_lock;//互斥锁pthread_cond_t cond;//条件变量void thread_clean(void *arg){ pthread_mutex_unlock(&m_lock);//清理异常中断的线程-->释放锁}void *my_fun1(void *arg){ thread_cleanup_push(thread_clean, NULL); while(1){ printf("my_fun1 is running\n"); pthread_mutex_lock(&m_lock); pthread_cond_wait(&cond, &m_lock); printf("func 1 cond worked\n"); pthread_mutex_unlock(&m_lock); sleep(2); } pthread_cleanup_pop(0);//清理异常中断的线程-->释放锁 return NULL;}void *my_fun2(void *arg){ while(1){ sleep(2); printf("my_fun2 is running\n"); pthread_mutex_lock(&m_lock); pthread_cond_wait(&cond, &m_lock);//检查是否有激活信号,没有则阻塞 printf("func 2 cond worked\n"); pthread_mutex_unlock(&m_lock); sleep(1); } return NULL;}int main(){ pthread_t tid1, tid2; printf("hey, this is main\n"); pthread_mutex_init(&m_lock, NULL); pthread_cond_init(&cond, NULL); pthread_create(&tid1, NULL, my_fun1, NULL); pthread_create(&tid2, NULL, my_fun2, NULL); while(1){ pthread_cancel(tid1);//取消线程tid1 sleep(2); pthread_cond_signal(&cond);//激活条件变量,若不激活,两线程将一直处于阻塞状态 } sleep(1); return 0;}
运行结果:
yu@ubuntu:~/Linux/201$ ./10tiaojianvalhey, this is mainmy_fun1 is runningmy_fun2 is runningfunc 2 cond workedmy_fun2 is runningfunc 2 cond workedmy_fun2 is runningfunc 2 cond workedmy_fun2 is running^C
- Linux多线程—条件变量
- linux 多线程-条件变量
- Linux多线程 |互斥锁|条件变量
- Linux多线程编程-条件变量
- linux 多线程(条件变量)
- linux 多线程【2】条件变量
- linux多线程编程---条件变量
- linux多线程之条件变量
- linux 多线程(条件变量)
- Linux多线程编程-条件变量
- Linux多线程编程---条件变量
- 【Linux多线程同步】条件变量
- linux多线程之条件变量
- linux多线程之条件变量
- linux多线程-----条件变量、pthread_cond_xxx
- Linux--多线程条件变量使用
- Linux编程练习 --多线程4--条件变量
- Linux多线程编程下的条件变量
- cmd运行Java程序的问题
- LeetCode78. Subsets
- 学习笔记-----listview中含有togglebutton
- 最简单的基于FFMPEG的转码程序
- jQuery源码分析:如何解决jQuery、$关键字与其它脚本框架的冲突问题?
- Linux多线程—条件变量
- 观台大朱云汉教授《中国大陆的兴起与全球政治经济秩序的重组》有感
- 文章标题
- 《Learn Python the hard way》学习记录
- 第十章编程练习(4)
- 线程的同步之条件变量
- Linux IPC之共享内存
- 个人笔记 jq 01 jq的方式实现取消文本框只读
- 读书笔记摘要《程序员修炼之道》(不定期更新)