条件变量是利用线程间共享得全局变量进行同步的一种机制,主要包括两个动作:一个线程等待“条件变量的条件成立”而挂起;另一个线程使“条件成立”给出条件成立信号。为了防止竞争,条件变量得使用总是和一个互斥锁结合在一起。

1、创建和注销

条件变量和互斥锁一样,有两种创建方式,静态方式使用PTHREAD_COND_INITIALIZER,动态方式使用pthread_coud_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)函数,cond_attr设置为NULL即可。注销需要使用int pthread_cond_destroy(pthread_cond_t *cond);

2、等待和激发

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

timewait方式表示超过时间条件没有满足则返回ETIMEOUT,结束等待 ,这个time是以绝对时间形式出现,即0表示格林尼治时间1970年1月1日0时0分0秒。为了防止多个线程同时请求pthread_cond_wait(),需要用互斥锁(mutex)来限定phread_cond_wait()的操作。对cond的操作必须是互斥的。下面是配合pthread_cleanup_push(),pthread_cleanup_pop()的一个示例程序:

#include "pthread.h"
#include "unistd.h"
#include "stdio.h"
#include "stdlib.h"
#include "string.h"struct _Node
{int number;struct _Node *next;
};
typedef struct _Node Node;Node *head;/*信号量和条件变量*/
static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t cond = PTHREAD_COND_INITIALIZER;/*线程异常时处理函数*/
static void cleanup_handler(void *arg)
{printf("cleanup_handler\n");free(arg);(void)pthread_mutex_unlock(&mtx);
}/*
pthread_cleanup_push注册一个回调函数,如果你的线程在对应的pthread_cleanup_pop之前异常退出(return是正常退出,其他是异常),那么系统就会执行这个回调函数(回调函数要做什么你自己决定)。但是如果在pthread_cleanup_pop之前没有异常退出,pthread_cleanup_pop就把对应的回调函数取消了
*/
static void* thread_func(void *arg)
{Node *p = NULL;printf("in thread_func()\n");/*注册线程异常处理函数*/pthread_cleanup_push(cleanup_handler, p);while (1){pthread_mutex_lock(&mtx);while (NULL != head){/*如果条件不满足,则挂起*/pthread_cond_wait(&cond, &mtx);p = head;printf("Got %d from front of queue\n",p->number);free(p);pthread_mutex_unlock(&mtx);}}/*清空线程异常处理函数*/pthread_cleanup_pop(0);return 0;
}int main()
{pthread_t tid;int i;Node *p;pthread_create(&tid, NULL, thread_func, NULL);for(i = 0; i < 10; i++){printf("int main():%d", i);p = (Node*)malloc(sizeof(Node));p->number = i;pthread_mutex_lock(&mtx);p->next = head;head = p;/*通知条件OK了*/pthread_cond_signal(&cond);pthread_mutex_unlock(&mtx);sleep(1);}printf("thread 1 wanna end then cancel thread2.\n");pthread_cancel(tid);pthread_join(tid, NULL);printf("All done\n");return 0;
}

输出结果如下:

int main():0in thread_func()
int main():1Got 1 from front of queue
int main():2Got 2 from front of queue
int main():3Got 3 from front of queue
int main():4Got 4 from front of queue
int main():5Got 5 from front of queue
int main():6Got 6 from front of queue
int main():7Got 7 from front of queue
int main():8Got 8 from front of queue
int main():9Got 9 from front of queue
thread 1 wanna end then cancel thread2.
cleanup_handler
All done

转载于:https://www.cnblogs.com/binmaizhai/archive/2013/03/21/2973554.html

多线程条件变量(pthread_cond_wait)用法相关推荐

  1. Linux Qt使用POSIX多线程条件变量、互斥锁(量)

    今天团建,但是文章也要写.酒要喝好,文要写美,方为我辈程序员的全才之路.嘎嘎 之前一直在看POSIX的多线程编程,上个周末结合自己的理解,写了一个基于Qt的用条件变量同步线程的例子.故此来和大家一起分 ...

  2. linux多进程条件变量,Linux 多线程条件变量同步

    条件变量是线程同步的另一种方式,实际上,条件变量是信号量的底层实现,这也就意味着,使用条件变量可以拥有更大的自由度,同时也就需要更加小心的进行同步操作.条件变量使用的条件本身是需要使用互斥量进行保护的 ...

  3. 5.3多线程条件变量

    多线程条件变量应用例子 Input_manager.h InputOpr 结构体添加 进程 ID :pthread_t t_TreadID; int AllInputDevicesInit(void) ...

  4. 条件变量pthread_cond_wait()和pthread_cond_signal()详解

    条件变量          条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起:另一个线程使"条件成立&qu ...

  5. 条件变量 pthread_cond_wait

    1.先了解一下等待队列.(默认大家了解mutex,如果不了解:https://blog.csdn.net/qq_33890670/article/details/79967231) 等待队列,是指li ...

  6. 多线程---条件变量

    互斥器和条件变量的区别:互斥器具有加锁原语,用来进行排他性的访问共享数据,而条件变量具有等待原语,用于等待某个事件的发生. 等待条件变量的正确姿势: void wait() {mutex.lock() ...

  7. Linux 条件变量 pthread_cond_wait

    条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起:另一个线程使"条件成立"(给出条件成立信号). ...

  8. 219-C++多线程(条件变量)

    操作1(不加线程) 操作2(引入多线程) 打印的结果不是我们想要的! 这里造成结果混乱的原因是什么? 原因是: 并发执行,多线程抢占资源. 这是一种异步执行方案.当我们程序链接编译通过之后,我们从主函 ...

  9. linux 多线程条件变量,linux多线程之条件变量

    假设有共享的资源sum,与之相关联的mutex 是lock_s.假设每个线程对sum的操作很简单的,与sum的状态无关,比如只是sum++.那么只用mutex足够了.程序员只要确保每个线程操作前,取得 ...

最新文章

  1. 【Android 安装包优化】Android 中使用 SVG 图片 ( Android 5.0 以下的矢量图方案 | 矢量图生成为 PNG 图片 )
  2. 想要求职Web安全相关的岗位,你就必须要懂的知识
  3. 石油采集(求联通区域) 2018多校寒假集训 (dfs+二分匹配)
  4. [2020-11-30 contest]数列(矩阵加速),秘密通道(dijkstra最短路)小X游世界树(换根dp),划分(数学)
  5. 信息学奥赛一本通 1155:回文三位数
  6. 使用HTML5 canvas做地图(1)基础知识
  7. Java 集成开发环境 Eclipse 安装
  8. 图片内包含文本制作方法
  9. heidisql 命令保存blob_git常用命令总结
  10. laravel-echo-server 不接收失败_6所高校公布报名不合格名单!这些问题最容易出错...
  11. java怎么设置窗体title_自定义Java窗口标题栏菜单
  12. VOC2007数据集解析
  13. 计算机的网络技术说课稿模板,精选信息技术说课稿模板汇编五篇
  14. 服务器如何修改vt,如何设置VT?
  15. python上的包嗅探
  16. PIPI OJ 1203: PIPI发工资(拓扑排序)
  17. 数据压缩作业1之:使用音频分析软件(Audacity)分析浊音、清音爆破音的时域及频域特性。
  18. echarts折线图曲线,每个值上面添加小圆点或者小圆圈
  19. PCB设计新手入门须知
  20. 请问PMP证书值得考吗?

热门文章

  1. shell执行perl_【编程技巧(一)】在Perl、Shell和Python中传参与输出帮助文档
  2. ccpc河北大学生程序设计竞赛dp小总结
  3. (十八)深入浅出TCPIP之HTTP和HTTPS
  4. 日志级别 debug info warn eirror fatal
  5. 密码学专题 证书和CA指令 申请证书|建立CA|CA操作|使用证书|验证证书
  6. 计算机操作系统生产者和消费者模型的简单介绍
  7. 安装solc模块4.25版本
  8. Java Stream MapReduce大数据开发模型
  9. Linux中10个有用的命令行补齐命令
  10. 移动互联网开始降温:“人才热”退烧