linux C++ 多线程使用pthread_cond 条件变量
1. 背景
多线程中经常需要使用到锁(pthread_mutex_t)来完成多个线程之间的互斥操作。
但是互斥锁有一个明显到缺点: 只有两种状态,锁定和非锁定。
而条件变量则通过允许线程阻塞并等待另一个线程发送唤醒信号的方法弥补了互斥锁的不足,它常和互斥锁一起使用。
2. 条件变量涉及到的主要函数
2.1 pthread_cond_wait 线程阻塞在条件变量
int pthread_cond_wait(pthread_cond_t *cv, pthread_mutex_t *mutex);
函数将解锁mutex参数指向的互斥锁,并使当前线程阻塞在cv参数指向的条件变量上。
被阻塞的线程可以被pthread_cond_signal函数,pthread_cond_broadcast函数唤醒,也可能在被信号中断后被唤醒。
pthread_cond_wait函数返回时,相应的互斥锁将被当前线程锁定,即使是函数出错返回。
pthread_cond_wait函数的返回并不意味着条件的值一定发生了变化,必须重新检查条件的值。
最好的测试方法是循环调用pthread_cond_wait函数,并把满足条件的表达式置为循环的终止条件。
pthread_mutex_lock();
while (condition_is_false)
pthread_cond_wait();
pthread_mutex_unlock();
阻塞在同一个条件变量上的不同线程被唤醒的次序是不一定的。
2.2 pthread_cond_signal 线程被唤醒
int pthread_cond_signal(pthread_cond_t *cv);
函数被用来释放被阻塞在指定条件变量上的一个线程。
必须在互斥锁的保护下使用相应的条件变量。否则对条件变量的解锁有可能发生在锁定条件变量之前,从而造成死锁。
唤醒阻塞在条件变量上的所有线程的顺序由调度策略决定,如果线程的调度策略是SCHED_OTHER类型的,系统将根据线程的优先级唤醒线程。
如果没有线程被阻塞在条件变量上,那么调用pthread_cond_signal()将没有作用。
更多函数可以看: http://blog.csdn.net/icechenbing/article/details/7662026
3. 实例代码
实现功能: 2个线程对count每次分别加1, 第三个线程等count大于10后一次加100.
3.1 加1线程函数
void *inc_count(void *idp)
{
int i = ;
int taskid = ;
int *my_id = (int*)idp; for (i=; i<TCOUNT; i++) {
pthread_mutex_lock(&count_mutex);
taskid = count;
count++; /*
唤醒一个阻塞在该条件变量到线程
如果没有线程被阻塞在条件变量上,那么调用pthread_cond_signal()将没有作用
*/
pthread_cond_signal(&count_threshold_cv); printf("inc_count(): thread %d, count = %d, unlocking mutex\n", *my_id, count);
pthread_mutex_unlock(&count_mutex);
sleep();
}
printf("inc_count(): thread %d, Threshold reached.\n", *my_id); pthread_exit(NULL);
}
3.2 count满足条件后, 单次加100函数
void *watch_count(void *idp)
{
int *my_id = (int*)idp;
printf("Starting watch_count(): thread %d\n", *my_id); pthread_mutex_lock(&count_mutex);
while(count<COUNT_LIMIT) {
sleep();
/*
函数将自动/原子的解锁count_mutex参数指向的互斥锁,并使当前线程阻塞在cv参数指向的条件变量上
被阻塞的线程可以被pthread_cond_signal函数,pthread_cond_broadcast函数唤醒,也可能在被信号中断后被唤醒
pthread_cond_wait函数的返回并不意味着条件的值一定发生了变化,必须重新检查条件的值.
本例子中线程被唤醒后, 仍然在while内会再次判断COUNT_LIMIT是否满足条件的值
pthread_cond_wait函数返回时,相应的互斥锁将被当前线程锁定,即使是函数出错返回
*/
pthread_cond_wait(&count_threshold_cv, &count_mutex);
printf("watch_count(): thread %d Condition signal received.\n", *my_id);
} count += ;
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
}
3.3 整体代码
#include <pthread.h>
#include <stdio.h>
#include <unistd.h> #define NUM_THREADS 3
#define TCOUNT 10
#define COUNT_LIMIT 10 int count = ;
int thread_ids[] = {,,};
pthread_mutex_t count_mutex;
pthread_cond_t count_threshold_cv; void *inc_count(void *idp)
{
int i = ;
int taskid = ;
int *my_id = (int*)idp; for (i=; i<TCOUNT; i++) {
pthread_mutex_lock(&count_mutex);
taskid = count;
count++; /*
唤醒一个阻塞在该条件变量到线程
如果没有线程被阻塞在条件变量上,那么调用pthread_cond_signal()将没有作用
*/
pthread_cond_signal(&count_threshold_cv); printf("inc_count(): thread %d, count = %d, unlocking mutex\n", *my_id, count);
pthread_mutex_unlock(&count_mutex);
sleep();
}
printf("inc_count(): thread %d, Threshold reached.\n", *my_id); pthread_exit(NULL);
} void *watch_count(void *idp)
{
int *my_id = (int*)idp;
printf("Starting watch_count(): thread %d\n", *my_id); pthread_mutex_lock(&count_mutex);
while(count<COUNT_LIMIT) {
sleep();
/*
函数将自动/原子到解锁mutex参数指向的互斥锁,并使当前线程阻塞在cv参数指向的条件变量上
被阻塞的线程可以被pthread_cond_signal函数,pthread_cond_broadcast函数唤醒,也可能在被信号中断后被唤醒
pthread_cond_wait函数的返回并不意味着条件的值一定发生了变化,必须重新检查条件的值.
本例子中使用类COUNT_LIMIT最为满足条件的值
pthread_cond_wait函数返回时,相应的互斥锁将被当前线程锁定,即使是函数出错返回
*/
pthread_cond_wait(&count_threshold_cv, &count_mutex);
printf("watch_count(): thread %d Condition signal received.\n", *my_id);
} count += ;
pthread_mutex_unlock(&count_mutex);
pthread_exit(NULL);
} int main (int argc, char *argv[])
{
int i, rc;
pthread_t threads[];
pthread_attr_t attr; /* Initialize mutex and condition variable objects */
pthread_mutex_init(&count_mutex, NULL);
pthread_cond_init (&count_threshold_cv, NULL); /* For portability, explicitly create threads in a joinable state */
pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_JOINABLE);
pthread_create(&threads[], &attr, inc_count, (void *)&thread_ids[]);
pthread_create(&threads[], &attr, inc_count, (void *)&thread_ids[]);
pthread_create(&threads[], &attr, watch_count, (void *)&thread_ids[]); /* Wait for all threads to complete */
for (i=; i<NUM_THREADS; i++) {
pthread_join(threads[i], NULL);
}
printf ("Main(): Waited on %d threads. Done.\n", NUM_THREADS); /* Clean up and exit */
pthread_attr_destroy(&attr);
pthread_mutex_destroy(&count_mutex);
pthread_cond_destroy(&count_threshold_cv);
pthread_exit(NULL); return ;
}
linux C++ 多线程使用pthread_cond 条件变量的更多相关文章
- linux多线程同步pthread_cond_XXX条件变量的理解
在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-- ...
- 详解linux互斥锁 pthread_mutex和条件变量pthread_cond
[cpp] view plaincopy ============================================================= int pthread_creat ...
- Linux多线程编程的条件变量
在stackoverflow上看到一关于多线程条件变量的问题,题主问道:什么时候会用到条件变量,mutex还不够吗?有个叫slowjelj的人做了很好的回答,我再看这个哥们其他话题的一些回答,感觉水平 ...
- Linux 多线程编程—使用条件变量实现循环打印
编写一个程序,开启3个线程,这3个线程的ID分别为A.B.C,每个线程将自己的ID在屏幕上打印10遍,要求输出结果必须按ABC的顺序显示:如:ABCABC….依次递推. 使用条件变量来实现: #inc ...
- 多线程编程中条件变量和的spurious wakeup 虚假唤醒
1. 概述 条件变量(condition variable)是利用共享的变量进行线程之间同步的一种机制.典型的场景包括生产者-消费者模型,线程池实现等. 对条件变量的使用包括两个动作: 1) 线程等待 ...
- linux网络编程之posix条件变量
今天来学习posix的最后一个相关知识----条件变量,言归正传. 下面用一个图来进一步描述条件变量的作用: 为什么呢? 这实际上可以解决生产者与消费者问题,而且对于缓冲区是无界的是一种比较理解的解决 ...
- 练习生产者与消费者-PYTHON多线程中的条件变量同步-Queue
以前练习过,但好久不用,手生,概念也生了, 重温一下.. URL: http://www.cnblogs.com/holbrook/tag/%E5%A4%9A%E7%BA%BF%E7%A8%8B/ ~ ...
- 四十二、Linux 线程——线程同步之条件变量之线程状态转换
42.1 线程状态转换 42.1.1 状态转换图 42.1.2 一个线程计算,多个线程获取的案例 #include <stdio.h> #include <stdlib.h> ...
- 四十一、Linux 线程——线程同步之条件变量
41.1 概念 41.1.1 条件变量的介绍 互斥锁的缺点是它只有两种状态:锁定和非锁定 条件变量通过允许线程阻塞和等待另一个线程发送信号的方法弥补了互斥锁的不足 条件变量内部是一个等待队列,放置等待 ...
随机推荐
- 算法初级面试题01——认识时间复杂度、对数器、 master公式计算时间复杂度、小和问题和逆序对问题
虽然以前学过,再次回顾还是有别样的收获~ 认识时间复杂度 常数时间的操作:一个操作如果和数据量没有关系,每次都是固定时间内完成的操作,叫做常数操作. 时间复杂度为一个算法流程中,常数操作数量的指标.常 ...
- Xamarin iOS教程之添加和定制视图
Xamarin iOS教程之添加和定制视图 Xamarin iOS用户界面——视图 在iPhone或者iPad中,用户看到的摸到的都是视图.视图是用户界面的重要组成元素.例如,想要让用户实现文本输入时 ...
- python中关于if-else使用性能的一点感悟
今天做leetcode第7题关于数字倒序的问题,分别使用如下程序:(72ms) class Solution: def reverse(self, x): """ :ty ...
- Luogu1445 [Violet]樱花 ---- 数论优化
Luogu1445 [Violet]樱花 一句话题意:(本来就是一句话的) 求方程 $\frac{1}{X} + \frac{1}{Y} = \frac{1}{N!}$ 的正整数解的组数,其中$N \ ...
- 图的遍历 之 深搜dfs
DFS 遍历 深度优先搜索是一个递归过程,有回退过程. 对一个无向连通图,在访问图中某一起始顶点u 后,由u 出发,访问它的某一邻接顶点v1:再从v1 出发,访问与v1 邻接但还没有访问过的顶点v2: ...
- Wannafly挑战赛24游记
Wannafly挑战赛24游记 A - 石子游戏 题目大意: A和B两人玩游戏,总共有\(n(n\le10^4)\)堆石子,轮流进行一些操作,不能进行下去的人则输掉这局游戏.操作包含以下两种: 把石子 ...
- BZOJ3779 : 重组病毒
一个点的感染时间为它到根路径上虚边数+1. 用Link-Cut Tree模拟虚实边切换,每次切换时等价于在一段或两段DFS序区间更新,线段树维护即可. 时间复杂度$O(n\log^2n)$. #inc ...
- Codeforces Round #394 (Div. 2) E. Dasha and Puzzle 构造
E. Dasha and Puzzle 题目连接: http://codeforces.com/contest/761/problem/E Description Dasha decided to h ...
- URAL 1970 J - 皇后像廣場 dfs
J - 皇后像廣場 题目连接: http://acm.hust.edu.cn/vjudge/contest/123332#problem/J Description Vova was walking ...
- GIT(7)----强制用远程代码覆盖本地修改
清除本地修改 git reset --hard 拉代码 git pull Git Pull While Ignoring Local Changes? git pull 并强制覆盖本地修改