linux 多线程编程 互斥锁与条件变量
来源:互联网 发布:windows insider下载 编辑:程序博客网 时间:2024/05/01 23:35
条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待 “ 条件变量的条件成立” 而挂起,另一个线程使 “条件成立 ”(给出条件成立信号),为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
下面这个例子展示的是互斥锁和条件变量的结合使用,以及取消对于条件等待动作的影响,例子中有两个线程被启动,并等待同一个条件变量。
#include<stdio.h>#include<pthread.h>#include<unistd.h>pthread_mutex_t mutex; // 互斥量pthread_cond_t cond; // 条件变量void* child1( void* param ){ pthread_cleanup_push( (void (*)(void*))pthread_mutex_unlock, (void*)&mutex ); while( 1 ) { printf( "thread 1 get running\n" ); printf( "thread 1 pthread_mutex_lock returns %d\n", pthread_mutex_lock( &mutex ) ); pthread_cond_wait( &cond, &mutex ); // 无条件等待 printf( "thread 1 condition applied\n" ); pthread_mutex_unlock( &mutex ); sleep( 5 ); } pthread_cleanup_pop( 0 ); return ( void* )0;}void* child2( void* param ){ while( 1 ) { sleep( 3 ); printf( "thread 2 get running\n" ); printf( "thread 2 pthread_mutex_lock returns %d\n", pthread_mutex_lock( &mutex ) ); pthread_cond_wait( &cond, &mutex ); // 无条件等待 printf( "thread 2 condition applied\n" ); pthread_mutex_unlock( &mutex ); sleep( 1 ); } return ( void* )0;}int main(){ pthread_t tid1, tid2; printf( "hello, condition variable test\n" ); pthread_mutex_init( &mutex, NULL ); // 初始化 互斥锁 pthread_cond_init( &cond, NULL ); // 初始化 条件变量 pthread_create( &tid1, NULL, child1, NULL ); // new 线程 tid1 pthread_create( &tid2, NULL, child2, NULL ); // new 线程 tid2 do { sleep( 2 ); pthread_cancel( tid1 ); // 发送终止信号给 tid1,具体请google 取消点 cancel point sleep( 2 ); pthread_cond_signal( &cond ); // 激活一个等待该条件的线程 }while( 1 ); sleep( 100 ); pthread_exit( 0 ); return 0;}
// pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mutex,然后阻塞在等待队列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mutex);,再读取资源
// 编译
kennie@cbib:~/pthreadDIR$ g++ -lpthread -o mutex_con.out mutex_con.cpp
// 运行结果
hello, condition variable testthread 1 get runningthread 1 pthread_mutex_lock returns 0thread 2 get runningthread 2 pthread_mutex_lock returns 0thread 2 condition appliedthread 2 get runningthread 2 pthread_mutex_lock returns 0thread 2 condition appliedthread 2 get runningthread 2 pthread_mutex_lock returns 0thread 2 condition appliedthread 2 get runningthread 2 pthread_mutex_lock returns 0thread 2 condition appliedthread 2 get runningthread 2 pthread_mutex_lock returns 0thread 2 condition appliedthread 2 get runningthread 2 pthread_mutex_lock returns 0thread 2 condition applied......
- linux 多线程编程 互斥锁与条件变量
- 【Linux C 多线程编程】互斥锁与条件变量
- Linux C 多线程编程之互斥锁与条件变量
- Linux C 多线程编程 互斥锁与条件变量
- Linux C 多线程编程 互斥锁与条件变量
- 嵌入式 Linux C 多线程编程 互斥锁与条件变量
- 【Linux C 多线程编程】互斥锁与条件变量
- Linux C 多线程编程 互斥锁与条件变量
- Linux C 多线程编程 -- 互斥锁与条件变量
- 【Linux C 多线程编程】互斥锁与条件变量
- Linux C 多线程编程----互斥锁与条件变量-转
- 【Linux C 多线程编程】互斥锁与条件变量
- 【Linux C 多线程编程】互斥锁与条件变量
- Linux多线程编程-条件变量
- linux多线程编程---条件变量
- Linux多线程编程-条件变量
- Linux多线程编程---条件变量
- Linux多线程 互斥锁与条件变量使用
- Swing不足之处【GUI速度方面】
- 如何打开/生成initrd.img
- Android ApiDemos示例解析(10):App->Activity->QuickContactsDemo
- 让一个精灵变黑,像蒙板
- Pthon+Qt程序 打包
- linux 多线程编程 互斥锁与条件变量
- Android测试教程(15):AndroidTestCase示例
- Android OpenGL ES 开发教程(15):通用的矩阵变换指令
- C++基础知识
- 控件(九)——Repeater控件实现分页
- 一个男人程序-------悲哀!
- SSH初步整合尝试
- c++指针和引用区别
- SQL 游标小结