linux 多线程编程 互斥锁与条件变量
2012-05-26 15:28
423 查看
条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待 “ 条件变量的条件成立” 而挂起,另一个线程使 “条件成立 ”(给出条件成立信号),为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
下面这个例子展示的是互斥锁和条件变量的结合使用,以及取消对于条件等待动作的影响,例子中有两个线程被启动,并等待同一个条件变量。
// pthread_cond_wait会先解除之前的pthread_mutex_lock锁定的mutex,然后阻塞在等待队列里休眠,直到再次被唤醒(大多数情况下是等待的条件成立而被唤醒,唤醒后,该进程会先锁定先pthread_mutex_lock(&mutex);,再读取资源
// 编译
// 运行结果
hello, condition variable test
thread 1 get running
thread 1 pthread_mutex_lock returns 0
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
......
下面这个例子展示的是互斥锁和条件变量的结合使用,以及取消对于条件等待动作的影响,例子中有两个线程被启动,并等待同一个条件变量。
#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 test
thread 1 get running
thread 1 pthread_mutex_lock returns 0
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
thread 2 get running
thread 2 pthread_mutex_lock returns 0
thread 2 condition applied
......
相关文章推荐
- 【Linux C 多线程编程】互斥锁与条件变量
- Linux C 多线程编程 互斥锁与条件变量
- (转)Linux C 多线程编程----互斥锁与条件变量
- 【Linux C 多线程编程】互斥锁与条件变量
- 【Linux C 多线程编程】互斥锁与条件变量
- Linux C 多线程编程 互斥锁与条件变量
- 【Linux C 多线程编程】互斥锁与条件变量
- Linux操作系统下的多线程编程详细解析----条件变量pthread_cond_t那些事儿
- Linux操作系统下的多线程编程详细解析----条件变量
- Linux C 多线程编程 互斥锁
- 转载_Linux多线程编程详细解析----条件变量 pthread_cond_t
- Linux多线程编程之线程结合互斥锁实现同步
- Linux多线程编程详细解析----条件变量 pthread_cond_t
- Linux多线程编程入门-同步机制-条件变量
- 【Linux C 多线程编程】互斥锁与条件变量
- Linux多线程消费者和生产者模型实例(互斥锁和条件变量使用)
- linux多线程编程--信号量和条件变量 唤醒丢失事件
- Linux多线程编程详解----条件变量pthread_cond_t
- 嵌入式 Linux C 多线程编程 互斥锁与条件变量
- Linux下多线程编程之互斥锁、条件变量、信号量