1 条件变量认识

(1)大家可能知道互斥量是线程程序中必须的工具了,但是也不能是万能的,就比如某个线程正在等待共享数据某个条件的发生,这个时候会发生什么呢。它就可能重复的尝试对互斥对象锁定和解锁来检查共享数据结构。

(2)线程在等待满足某些条件的时候使线程进入睡眠状态,一旦条件满足了就唤醒并等待满足特定条件而睡眠的线程。

(3)条件变量一般都允许线程阻塞和等待另一个线程发送信号的方法来弥补互斥锁的不足。

2 函数介绍

(1) 静态方式使用 pthread_cond_t cond=PTHREAD_COND_INITIALIZER

动态方式 int pthread_cond_init(pthread_cond_t *cond,pthread_condattr_t *cond_attr)

-------->当cond_attr为null的时候使用默认的属性。

(2)注销一个条件变量

int pthread_cond_destroy(pthread_cond_t *cond)只有没有线程在这个条件变量的时候才能注销这个条件变量。

(3)等待有两种方式,条件等待和时间等待。无论哪种等待都必须和一个互斥锁配合来防止多个线程同时请求pthread_cond_wait()这个竞争条件。

(4)激发 两种方式,pthread_cond_signal()激活一个等待该条件的线程。pthread_cond_broadcast()激活所有等待线程。

3 例子

(1)第一个例子

 #include <iostream>
#include <pthread.h>
#include <unistd.h>
using namespace std;
pthread_cond_t qready = PTHREAD_COND_INITIALIZER; /*初始构造条件变量*/
pthread_mutex_t qlock = PTHREAD_MUTEX_INITIALIZER; /*初始构造锁*/
int x = 10;
int y = 20;
void *func1(void *arg){
cout<<"func1 开始"<<endl;
pthread_mutex_lock(&qlock);
while(x<y)
{
pthread_cond_wait(&qready,&qlock);
}
pthread_mutex_unlock(&qlock);
sleep(3);
cout<<"func1 结束"<<endl;
}
void *func2(void *arg){
cout<<"func2 开始"<<endl;
pthread_mutex_lock(&qlock);
//修改xy
x = 20;
y = 10;
cout<<"has change x and y"<<endl;
pthread_mutex_unlock(&qlock);
if(x > y){
pthread_cond_signal(&qready);//发送信号 使线程1不阻塞
}
cout<<"func2 结束"<<endl;
}
int main(int argc,char **argv){
pthread_t tid1,tid2;
int iRet;
iRet = pthread_create(&tid1,NULL,func1,NULL);
if(iRet){
cout<<"pthread 1 create error"<<endl;
return iRet;
}
sleep(2);
iRet = pthread_create(&tid2,NULL,func2,NULL);
if(iRet){
cout<<"pthread 2 create error"<<endl;
return iRet;
}
sleep(5);
return 0;
}

(2)第二个例子

 #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <pthread.h>
#include <errno.h>
#include <iostream>
#include <pthread.h>
using namespace std; /*提示出租车到达的条件变量*/
pthread_cond_t taxiCond = PTHREAD_COND_INITIALIZER;
/*同步锁*/
pthread_mutex_t taxiMutex = PTHREAD_MUTEX_INITIALIZER; int travelerCound=0;
//旅客到来的时候 数量加上1
void * traveler_arrive(void * name){
cout<<"Traveler: "<<(char *)name<<" needs a taxi now!"<<endl;
pthread_mutex_lock(&taxiMutex);
travelerCound++;
pthread_cond_wait(&taxiCond,&taxiMutex);
pthread_mutex_unlock(&taxiMutex);
cout<<"Traveler: "<<(char *)name<<" now got a taxi!"<<endl;
pthread_exit((void*)0);
} void * taxi_arrive(void * name){
cout<<"Taxi: "<<(char *)name<<" arrives."<<endl;
//保证先来的检测是否有新的顾客到达
while(1){
pthread_mutex_lock(&taxiMutex);
//大于则通知
if(travelerCound>0){
pthread_cond_signal(&taxiCond);
pthread_mutex_unlock(&taxiMutex);
break;
}
pthread_mutex_unlock(&taxiMutex);
}
pthread_exit((void*)0);
} int main(){
pthread_t tids[3];
int iRet = pthread_create(&tids[0],NULL,taxi_arrive,(void*)(" Jack "));
if(iRet){
printf("pthread_create error: iRet=%d\n",iRet);
return iRet;
}
printf("Time passing by.\n");
sleep(1);
iRet = pthread_create(&tids[1],NULL,traveler_arrive,(void*)(" Susan "));
if(iRet){
printf("pthread_create error: iRet=%d\n",iRet);
return iRet;
}
printf("Time passing by.\n");
sleep(1);
iRet = pthread_create(&tids[2],NULL,taxi_arrive,(void*)(" Mike "));
if(iRet){
printf("pthread_create error: iRet=%d\n",iRet);
return iRet;
}
printf("Time passing by.\n");
sleep(1); void *retval;
for(int i=0;i<3;i++){
iRet=pthread_join(tids[i],&retval);
if (iRet){
printf("pthread_join error: iRet=%d\n",iRet);
return iRet;
}
printf("retval=%ld\n",(long)retval);
}
return 0;
}

好叻 加油!!!!

linux中的条件变量的更多相关文章

  1. 在SQL存储过程中给条件变量加上单引号

    在SQL存储过程中给条件变量加上单引号,不加语句就会出问题,以下就是在存储过程中将条件where设置成了动态变化的,给where赋完值再和前面的语句拼接,再execute(SQL) ), )), )+ ...

  2. Linux中PATH环境变量的作用和使用方法

    关于PATH的作用:PATH说简单点就是一个字符串变量,当输入命令的时候LINUX会去查找PATH里面记录的路径.比如在根目录/下可以输入命令ls,在/usr目录下也可以输入ls,但其实ls这个命令根 ...

  3. [转帖]Linux教程(20)- Linux中的Shell变量

    Linux教程(20)- Linux中的Shell变量 2018-08-24 11:30:16 钱婷婷 阅读数 37更多 分类专栏: Linux教程与操作 Linux教程与使用   版权声明:本文为博 ...

  4. linux多线程-互斥&条件变量与同步

    多线程代码问题描述 我们都知道,进程是操作系统对运行程序资源分配的基本单位,而线程是程序逻辑,调用的基本单位.在多线程的程序中,多个线程共享临界区资源,那么就会有问题: 比如 #include < ...

  5. Linux系统编程—条件变量

    条件变量是用来等待线程而不是上锁的,条件变量通常和互斥锁一起使用.条件变量之所以要和互斥锁一起使用,主要是因为互斥锁的一个明显的特点就是它只有两种状态:锁定和非锁定,而条件变量可以通过允许线程阻塞和等 ...

  6. Linux有问必答:如何在Linux中修改环境变量PATH

    提问: 当我试着运行一个程序时,它提示“command not found”. 但这个程序就在/usr/local/bin下.我该如何添加/usr/local/bin到我的PATH变量下,这样我就可以 ...

  7. 练习生产者与消费者-PYTHON多线程中的条件变量同步-Queue

    以前练习过,但好久不用,手生,概念也生了, 重温一下.. URL: http://www.cnblogs.com/holbrook/tag/%E5%A4%9A%E7%BA%BF%E7%A8%8B/ ~ ...

  8. Linux中的环境变量PATH

    一.介绍 在讲环境变量之前,先介绍一下命令which,它用于查找某个命令的绝对路径,示例如下: 在上面的示例中,用which查到rm命令的绝对路径为/usr/bin/rm. 那么问题来了:为什么我们使 ...

  9. Linux线程同步——条件变量

    互斥锁是用来给资源上锁的,而条件变量是用来等待而不是用来上锁的. 条件变量用来自动阻塞一个线程,直到某特殊情况发生为止. 通常条件变量和互斥锁同时使用. 和条件变量使用有关的几个重要函数: int p ...

随机推荐

  1. Laravel建站05--缓存、时间日期处理包

    缓存 Laravel 给多种缓存系统提供丰富而统一的 API,缓存配置信息位于 config/cache.php,在这个文件中你可以为你的应用程序指定默认的缓存驱动,Laravel 支持当前流行的缓存 ...

  2. Jquery 插件 实例

    先说明下应用场景,通过可配项的配置和默认项覆盖,获取指定的需求数据,填充到指定的位置(两个指定其实都是可配的) (function($) { $.fn.extend({ getOneNews: fun ...

  3. 下载与安装---tensorflow on linux

    http://wiki.jikexueyuan.com/project/tensorflow-zh/get_started/os_setup.html 你可以使用我们提供的 Pip, Docker, ...

  4. 透视WPF 应用程序的利器

    当我们看到一些设计新颖的网站时,可以借助浏览器自带的Inspector 工具或插件方便的浏览网站布局结构及逻辑.如果是WPF 应用程序能否看到控件的架构方式呢?本篇将介绍两款工具Snoop 和WPF ...

  5. JS中小数如何转化为百分数并能四舍五入

    <script type="text/javascript">//n表示百分数保留的位数 function toPercent(n){ n = n || 2; retu ...

  6. Python 008- 游戏2048

    #-*- coding:utf-8 -*- import curses from random import randrange, choice # generate and place new ti ...

  7. 【BZOJ1419】Red is good 期望

    [BZOJ1419]Red is good Description 桌面上有R张红牌和B张黑牌,随机打乱顺序后放在桌面上,开始一张一张地翻牌,翻到红牌得到1美元,黑牌则付出1美元.可以随时停止翻牌,在 ...

  8. SAM4E单片机之旅——9、UART与MCK之MAINCK

    为得到更高的带宽,需要使用更高的波特率.UART波特率的计算已经介绍过了,现在就尝试下调整外设的时钟频率.可以有多种方法调整外设时钟(MCK)的频率,这里先介绍先主要时钟(MAINCK)的设置,其中包 ...

  9. wepy原理研究

    像VUE一样写微信小程序-深入研究wepy框架 https://zhuanlan.zhihu.com/p/28700207 wepy原理研究 虽然wepy提升了小程序开发体验,但毕竟最终要运行在小程序 ...

  10. NYOJ-37 回文字符串 —— LCS变形

    题目链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=37 题解: 一开始想从两边向中间添加字符,发现这样不是最优的.因为加入字符之后,这些原本存 ...