条件变量 条件缩 pthread_cond_t
来源:互联网 发布:mysql 表索引 编辑:程序博客网 时间:2024/04/28 12:41
说明,
等待线程
1。使用pthread_cond_wait前要先加锁
2。pthread_cond_wait内部会解锁,然后等待条件变量被其它线程激活
3。pthread_cond_wait被激活后会再自动加锁
激活线程:
1。加锁(和等待线程用同一个锁)
2。pthread_cond_signal发送信号
3。解锁
激活线程的上面三个操作在运行时间上都在等待线程的pthread_cond_wait函数内部。
程序示例:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t count_lock;
pthread_cond_t count_nonzero;
unsigned count = 0;
void * decrement_count(void *arg) {
pthread_mutex_lock (&count_lock);
printf("decrement_count get count_lock/n");
while(count==0) {
printf("decrement_count count == 0 /n");
printf("decrement_count before cond_wait /n");
pthread_cond_wait( &count_nonzero, &count_lock);
printf("decrement_count after cond_wait /n");
}
count = count -1;
pthread_mutex_unlock (&count_lock);
}
void * increment_count(void *arg){
pthread_mutex_lock(&count_lock);
printf("increment_count get count_lock/n");
if(count==0) {
printf("increment_count before cond_signal/n");
pthread_cond_signal(&count_nonzero);
printf("increment_count after cond_signal/n");
}
count=count+1;
pthread_mutex_unlock(&count_lock);
}
int main(void)
{
pthread_t tid1,tid2;
pthread_mutex_init(&count_lock,NULL);
pthread_cond_init(&count_nonzero,NULL);
pthread_create(&tid1,NULL,decrement_count,NULL);
sleep(2);
pthread_create(&tid2,NULL,increment_count,NULL);
sleep(10);
pthread_exit(0);
}
等待线程
1。使用pthread_cond_wait前要先加锁
2。pthread_cond_wait内部会解锁,然后等待条件变量被其它线程激活
3。pthread_cond_wait被激活后会再自动加锁
激活线程:
1。加锁(和等待线程用同一个锁)
2。pthread_cond_signal发送信号
3。解锁
激活线程的上面三个操作在运行时间上都在等待线程的pthread_cond_wait函数内部。
程序示例:
#include <stdio.h>
#include <pthread.h>
#include <unistd.h>
pthread_mutex_t count_lock;
pthread_cond_t count_nonzero;
unsigned count = 0;
void * decrement_count(void *arg) {
pthread_mutex_lock (&count_lock);
printf("decrement_count get count_lock/n");
while(count==0) {
printf("decrement_count count == 0 /n");
printf("decrement_count before cond_wait /n");
pthread_cond_wait( &count_nonzero, &count_lock);
printf("decrement_count after cond_wait /n");
}
count = count -1;
pthread_mutex_unlock (&count_lock);
}
void * increment_count(void *arg){
pthread_mutex_lock(&count_lock);
printf("increment_count get count_lock/n");
if(count==0) {
printf("increment_count before cond_signal/n");
pthread_cond_signal(&count_nonzero);
printf("increment_count after cond_signal/n");
}
count=count+1;
pthread_mutex_unlock(&count_lock);
}
int main(void)
{
pthread_t tid1,tid2;
pthread_mutex_init(&count_lock,NULL);
pthread_cond_init(&count_nonzero,NULL);
pthread_create(&tid1,NULL,decrement_count,NULL);
sleep(2);
pthread_create(&tid2,NULL,increment_count,NULL);
sleep(10);
pthread_exit(0);
}
- 条件变量 条件缩 pthread_cond_t
- 线程条件变量pthread_cond_t
- 条件变量 pthread_cond_t
- pthread_cond_t条件变量
- 线程条件变量pthread_cond_t
- 线程条件变量pthread_cond_t
- 线程条件变量pthread_cond_t
- 线程条件变量pthread_cond_t
- 线程条件变量pthread_cond_t
- 条件变量pthread_cond_t
- 条件变量pthread_cond_t
- 条件变量pthread_cond_t怎么用
- 线程条件变量pthread_cond_t用法
- 条件变量pthread_cond_t怎么用
- 条件变量pthread_cond_t怎么用
- 条件变量pthread_cond_t怎么用
- 多线程编程-条件变量pthread_cond_t
- 条件变量pthread_cond_t怎么用
- Linux那些事儿 之 戏说USB(16)接口是设备的接口(一)
- 蛋糕保卫战(金钱无限版)
- Haxdoors of the Kaspersky Antivirus 6/7
- Linux系统下的多线程编程入门
- linux多线程设计
- 条件变量 条件缩 pthread_cond_t
- 35岁以前成功必须戒除的9大恶习
- 从今天开始正式成为深圳人
- 放不下的VC 6.0
- select-sort
- blog上添加天气预报等脚本
- 我是如何发现CCProxy远程溢出漏洞的
- 纯ajax+asp.net+sqlserver多方式分页
- 拿中国互联网的两个航母说事(六)--百度终极面试