原文地址:https://www.cnblogs.com/guxuanqing/p/10576691.html

近期学习了线程等待和激活的相关知识。

先介绍几个api:

pthread_cond_t表示多线程的条件变量,用于控制线程等待和就绪的条件。

一:条件变量的初始化:

条件变量和互斥锁一样,都有静态动态两种创建方式,

静态方式使用PTHREAD_COND_INITIALIZER常量初始化。

pthread_cond_t cond = PTHREAD_COND_INITIALIZER;

动态方式初始化:

1 首先要new或者malloc一个pthread_cond_t类型变量,

用完后记得delete或者free掉。

2

动态方式调用pthread_cond_init()函数,API定义如下:

int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr);

二:条件变量的销毁

注销一个条件变量需要调用pthread_cond_destroy(),只有在没有线程在该条件变量上等待的时候才能注销这个条件变量,否则返回EBUSY。

因为Linux实现的条件变量没有分配什么资源,所以注销动作只包括检查是否有等待线程。API定义如下:

int pthread_cond_destroy(pthread_cond_t *cond)

new开辟的pthread_cond_t记得在调用pthread_cond_destroy()后调用delete或者free销毁掉。

三:等待和触发

1条件等待

int pthread_cond_wait(pthread_cond_t * cond, pthread_mutex_t * mutex);

2时间等待

int pthread_cond_timedwait(pthread_cond_t *cond, pthread_mutex_t *mutex, const struct timespec *abstime);

其中计时等待方式如果在给定时刻前条件没有满足,则返回ETIMEOUT,结束等待,

其中abstime以与time()系统调用相同意义的绝对时间形式出现,0表示格林尼治时间1970年1月1日0时0分0秒。

无论哪种等待方式,都必须和一个互斥锁配合:

  1. 以防止多个线程同时请求pthread_cond_wait()(或pthread_cond_timedwait(),下同)的竞争条件(Race Condition)。
  2. 为了应对线程1在调用pthread_cond_wait()但线程1还没有进入wait cond 的状态的时候,此时线程2调用了cond_singal的情况。如果不用mutex锁的话, 这个cond_singal就丢失了。加了锁的情况是,线程2必须等到 mutex 被释放  (也就是 pthread_cod_wait() 进入wait_cond状态 并自动释放mutex)的时  候才能调用cond_singal(前提:线程2也使用mutex)。

mutex互斥锁必须是普通锁(PTHREAD_MUTEX_TIMED_NP)或者适应锁(PTHREAD_MUTEX_ADAPTIVE_NP),且在调用pthread_cond_wait()前必须由本线程加锁(pthread_mutex_lock()),而在更新条件等待队列以前,mutex保持锁定状态,并在线程挂起进入等待前解锁。

在条件满足从而离开pthread_cond_wait()之前,mutex将被重新加锁,以与进入pthread_cond_wait()前的加锁动作对应。

使用pthread_cond_wait方式如下:

pthread _mutex_lock(&mutex)

while或if(线程执行的条件是否成立)

pthread_cond_wait(&cond, &mutex);

线程执行

pthread_mutex_unlock(&mutex);

3

激发条件有两种形式,pthread_cond_signal()激活一个等待该条件的线程,存在多个等待线程时按入队顺序激活其中一个;

而pthread_cond_broadcast()则激活所有等待线程。

上面就是多线程条件变量的基础知识,下面着重阐述下为什么调用pthread_cond_wait之前要加锁,以及pthread_cond_wait内部

调用了什么。

首先解释为什么在等待前加锁,因为线程隶属于进程,线程共享进程的资源,如果不进行加锁,就会造成多个线程同时(相对意义的同时,

可能一个线程在函数A中更改此共享资源,此时函数A没结束,另一个线程也访问了这个共享资源)

访问这块共享的资源,如果对临界区的内容进行更改,那么两个线程就会造成数据的不准确。所以在更改临界资源的时候要枷锁。而调用

pthread_cond_wait之前要加锁也是为了避免多个线程竞争一个条件,造成共享的资源被多个线程更改。所以需要互斥的访问共有资源,

那么在pthread_cond_wait之前需要加锁,避免别的线程更改共有资源。

接下来思考pthread_cond_wait内部做了哪些操作。

在pthread_cond_wait调用之前,线程调用pthread_mutex_lock,设置锁,如果条件不满足,那么该线程处于阻塞等待的状态。别的线程

发现条件满足后会调用pthread_cond_signal或pthread_cond_broadcast通知他。那么问题出来了,如果该线程不解锁,别的线程是没办法

更改共享资源的,也就没办法设置条件变量使其满足该线程的等待条件,出现死锁。所以,pthread_cond_wait会在内部进行解锁操作。别的

线程可以访问共享资源,更改条件触发该线程,使该线程从阻塞状态变为就绪。慢一点,还有一个重要的步骤,pthread_cond_wait会将该线程

放到线程等待队列里,那么是在放到等待队列之前解锁还是放到等待队列之后才解锁呢?

对于这点apue给出的解释:The mutex passed to pthread_cond_wait protects the condition.The caller passes it locked to

the function, which then atomically places the calling thread on the list of threads waiting for the condition and unlocks

the mutex. This closes the window between the time that the condition is checked and the time that the

thread goes to sleep waiting for the condition to change, so that the thread doesn't miss a change in the condition.

When pthread_cond_wait returns, the mutex is again locked.

这段话的意思是mutex传递给pthread_cond_wait 用于保护条件,调用者将mutex传递给pthread_cond_wait,

pthread_cond_wait 会自动将调用该函数的线程放到线程等待队列上,等待条件并且解锁。这种做法关闭了一段间隙,

这段间隙就是在我们检测条件的时刻和将线程放到等待队列休眠的时刻之间,这么做该线程不会错过条件的改变。而当

pthread_cond_wait 返回时,mutex又被上锁了。

所以,pthread_cond_wait内部的操作顺序是将线程放到等待队列,然后解锁,等条件满足时进行加锁,然后返回。

整理下pthread_cond_wait内部操作

1,线程放在等待队列上,解锁

2,等待 pthread_cond_signal或者pthread_cond_broadcast信号之后去竞争锁

3,若竞争到互斥索则加锁。

使用流程

等待线程:

pthread_mutex_lock(&mutex);

if(条件不满足)

  pthread_cond_wait(&cond, &mutex);

//处理共享资源

pthread_mutex_unlock(&mutex);

激活线程:

pthread_mutex_lock(&mutex);

pthread_cond_signal(&cond);

pthread_mutex_unlock(&mutex);

下面写了一个例子

#include <pthread.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <iostream>
using namespace std;int count = 0;
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
//该函数增加count数值
void * creator(void * arg)
{cout << "creator add lock" << endl;pthread_mutex_lock(&mutex);count ++;cout << "in creator count is : " << count << endl;//条件满足时发送信号if(count > 0){pthread_cond_signal(&cond);}cout << "creator release lock" << endl;pthread_mutex_unlock(&mutex);return NULL;}//该函数减少count数值
void * consumer(void * arg)
{cout << "consumer add lock" << endl;pthread_mutex_lock(&mutex);//当条件不满足时等待if(count <= 0){cout << "begin wait" << endl;pthread_cond_wait(&cond,&mutex);cout << "end wait" << endl;}count --;cout << "in consumer count is " << count << endl;pthread_mutex_unlock(&mutex);cout << "consumer release lock" << endl;return NULL;}int main()
{//两个线程,一个生产者线程一个消费者线程pthread_t createthread,consumethread;pthread_create(&consumethread, NULL, consumer, NULL);sleep(2);pthread_create(&createthread, NULL, creator, NULL);//主进程等待两个线程结束pthread_join(createthread, NULL);pthread_join(consumethread, NULL);return 0;
}

因为消费者线程先跑起来,会等待生产者增加count数量,所以打印输出结果如下

下面将消费者和生产者线程增加几个,creater和consumer内部用循环处理,

这样就能看出效果了。

void * creator(void * arg)
{int i = 0;while(i<300){i++;cout << "creator add lock" << endl;pthread_mutex_lock(&mutex);count ++;cout << "in creator count is : " << count << endl;if(count > 0){pthread_cond_signal(&cond);}cout << "creator release lock" << endl;pthread_mutex_unlock(&mutex);}return NULL;}void * consumer(void * arg)
{int i = 0;while(i < 100){i++;cout << "consumer add lock" << endl;pthread_mutex_lock(&mutex);if(count <= 0){cout << "begin wait" << endl;pthread_cond_wait(&cond,&mutex);cout << "end wait" << endl;}count --;cout << "in consumer count is " << count << endl;pthread_mutex_unlock(&mutex);cout << "consumer release lock" << endl;}return NULL;}int main()
{pthread_t createthread[2],consumethread[3];for(int i = 0; i < 3; i++){pthread_create(&consumethread[i], NULL, consumer, NULL);}for(int i = 0; i < 2; i++){pthread_create(&createthread[i], NULL, creator, NULL);}for(int i = 0; i < 2; i++){pthread_join(createthread[i], NULL);}for(int i = 0; i < 3; i++){pthread_join(consumethread[i], NULL);}return 0;}

截取一部分结果截图,可以看出数字是连续变动的,而且

加锁解锁内数字才变动,说明我们对锁和条件变量使用合理

pthread _cond_wait()函数相关推荐

  1. 线程模型、pthread 系列函数 和 简单多线程服务器端程序

    一.线程有3种模型,分别是N:1用户线程模型,1:1核心线程模型和N:M混合线程模型,posix thread属于1:1模型. (一).N:1用户线程模型 "线程实现"建立在&qu ...

  2. linux中的线程函数

    函数pthread_create  作用:创建线程  函数原型:int pthread_create(pthread_t * tidp,const pthread_attr_t*attr,void*( ...

  3. Linux C/C++多线程pthread实例

    inux中C/C++开发多线程程序多遵循POSIX线程接口(也就是pthread),pthread涉及函数很多个(更多参见pthread.h头文件),常用的有pthread_create.pthrea ...

  4. Linux多线程编程-线程函数返回值(返回简单数据类型)

    引言 通过几个实验练习,学习线程之间连接的具体实现.下面列举了两个例子,一个是子线程返回简单数据类型:另一个是子线程返回复杂数据类型. 实现代码 子线程返回简单的数据类型 #include<st ...

  5. C++ pthread 多线程

    本篇是我在学习C++多线程的时候做的笔记,主要记录的是基础的流程,部分代码实例,以及重点函数的说明. pthread 入口函数类型说明 void * func1(void * t) void* 表示无 ...

  6. 简单了解线程pthread_create函数

    在Linux下实现服务器线程的操作 并发 通俗的并发通常是指同时能并行的处理多个任务. 同时拥有两个或者多个线程,如果程序在单核处理器上运行,多个线程将交替的换入或者换出内存,这些线程是同时" ...

  7. linux线程随笔-pthread_create函数

    函数简介 pthread_create是UNIX环境创建线程函数 头文件 #include<pthread.h> 函数声明 int pthread_create(pthread_t  *r ...

  8. Linux pthread_self和pthread_create函数

    pthread_self和pthread_create函数 头文件 #include <pthread.h> 函数原型 pthread_t pthread_self(void); int ...

  9. pthread_create函数详解

    函数简介 编辑 头文件 1 #include<pthread.h> 函数声明 1 2 int pthread_create(pthread_t *tidp,const pthread_at ...

最新文章

  1. H5与Native交互之JSBridge技术 1
  2. 《深入理解 Spring Cloud 与微服务构建》第一章 微服务简介
  3. iOS 程序 main函数之前发生什么
  4. unity头顶状态制作_Unity中结合IK实现Lookat
  5. flutter初体验之基础控件知识
  6. 【手绘】A old painting ,drawed in middle school ,grade 8
  7. 超全汇总,常见的芯片封装大全-道合顺大数据infinigo
  8. 删好友警告,C语言最强整人小程序!(勿随便使用)
  9. AI基础:深度强化学习之路
  10. 第12周 上机报告 1之练习3 回文日
  11. Professor Forcing: A New Algorithm for Training Recurrent Networks翻译
  12. Speedoffice(word)如何设置分栏
  13. 解决Android调试微信页面,chrome的inspect弹出空白
  14. Ubuntu下使用opera的坑
  15. 百田游戏2014笔试题——找到有序序列中某个值第一次出现的位置,并打印
  16. python获取二进制bit位_Python读字节某一位的值,设置某一位的值,二进制位操作...
  17. java poi读取word 2003, 2007文档
  18. 【调剂】安徽理工大学矿业工程学院接收调剂学硕研究生
  19. STM32F103RBT6型号说明及特征;
  20. 蓝桥杯 分巧克力 python

热门文章

  1. Latex下代码的排版
  2. 欲善其事,先利其器——青龙面板依赖安装教程
  3. 测试员,面对自己30岁后的下坡路,我们该何去何从?
  4. Firebase报错:Installations Service is unavailable. Please try again later.
  5. [大数据面试]--智力题(2)
  6. 【考研数学高数部分】无穷级数
  7. Unity初级案例-愤怒的小鸟:六:17把粒子系统显示在UI之前+18让星星一颗一颗的显示+19添加暂停动画
  8. 2020年中国包子行业现状及竞争格局分析,未来行业集中度将进一步提升「图」
  9. AD7606 SPI模式 网上问题汇总
  10. mac 访问局域网服务器地址