pthread_cond_t
条件锁pthread_cond_t
(1)pthread_cond_wait的使用
等待线程
1. 使用pthread_cond_wait前要先加锁
2. pthread_cond_wait内部会解锁,然后等待条件变量被其它线程激活
3. pthread_cond_wait被激活后会再自动加锁
(2)pthread_cond_signal的使用
激活线程:
1. 加锁(和等待线程用同一个锁)
2. pthread_cond_signal发送信号
3. 解锁
激活线程的上面三个操作在运行时间上都在等待线程的pthread_cond_wait函数内部。
#include <pthread.h>
#include <unistd.h>
#include <stdio.h>struct msg {
struct msg* next;
int data;
};
struct msg* tasks = NULL; pthread_mutex_t mu = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
void* TestWait(void* args) {
pthread_mutex_lock(&mu);//这里开始时加锁
while() {
pthread_cond_wait(&cond, &mu);//该函数首先将当前线程加入内部的等待列表中,然后释放锁,接着休眠并阻塞。。。。。直到有外部信号(pthread_cond_signal)通知,会加锁然后返回。
printf("%ld wake up\n", pthread_self());
if (tasks != NULL) {
struct msg* tsk = tasks;
tasks = tasks->next;
printf("tid:%ld data:%ld\n", pthread_self(), tsk->data);
free(tsk);
}
}
pthread_mutex_unlock(&mu);
pthread_exit(NULL);
} int main() { pthread_t tids[];
for(int i = ; i < ; i++) {
pthread_create(&tids[i], NULL, TestWait, NULL);
}
sleep(); for (int i = ; i < ; i++) {
pthread_mutex_lock(&mu); struct msg* tmp = (struct msg*) malloc(sizeof(struct msg));
tmp->next = tasks;
tmp->data = ;
tasks = tmp; pthread_cond_signal(&cond);
//pthread_cond_broadcast(&cond);
pthread_mutex_unlock(&mu);
} pthread_exit(NULL);
return ;
}
pthread_cond_t的更多相关文章
- 条件变量pthread_cond_t怎么用
#include <pthread.h> #include <stdio.h> #include <stdlib.h> pthread_mutex_t mutex ...
- 通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)
使用条件变量提高效率 本文是 POSIX 线程三部曲系列的最后一部分,Daniel 将详细讨论如何使用条件变量.条件变量是 POSIX 线程结构,可以让您在遇到某些条件时“唤醒”线程.可以将它们看作是 ...
- Linux多线程编程详细解析----条件变量 pthread_cond_t
Linux操作系统下的多线程编程详细解析----条件变量 1.初始化条件变量pthread_cond_init #include <pthread.h> int pthread_cond_ ...
- 关于一点pthread_cond_t条件锁的思考以及实验
转:http://blog.csdn.net/aniao/article/details/5802015 APUE上,关于条件锁.其中有这么几条总结: 1.使用条件锁前必须先锁住对应的互斥锁. 2.条 ...
- ZT 为什么pthread_cond_t要和pthread_mutex_t同时使用 || pthread/Linux多线程编程
为什么线程同步的时候pthread_cond_t要和pthread_mutex_t同时使用 (2009-10-27 11:07:23) 转载▼ 标签: 杂谈 分类: 计算机 举一个例子(http:// ...
- POSIX多线程编程-条件变量pthread_cond_t
条件变量通过允许线程阻塞和等待另一个线程发送信号的方法弥补了互斥锁的不足,它常和互斥锁一起使用.使用时,条件变量被用来阻塞一个线程,当条件不满足时,线程往往解开相应的互斥锁并等待条件发生变化.一旦其它 ...
- pthread_mutex_t & pthread_cond_t 总结
pthread_mutex_t & pthread_cond_t 总结 一.多线程并发 1.1 多线程并发引起的问题 我们先来看如下代码: #include <stdio.h> # ...
- int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restric mutex)
mutex:为了保护条件变量而存在的: cond:为了线程通信而存在的. 整个机制都是为了保护条件变量和线程间通信而存在的. pthread_cond_wait()函数一进入wait状态就会自动rel ...
- 《Note --- Unreal --- MemPro (CONTINUE... ...)》
Mem pro 是一个主要集成内存泄露检测的工具,其具有自身的源码和GUI,在GUI中利用"Launch" button进行加载自己待检测的application,目前支持的平台为 ...
随机推荐
- redis数据结构有哪些
1.String 可以是字符串,整数或者浮点数,对整个字符串或者字符串中的一部分执行操作,对整个整数或者浮点执行自增(increment)或者自减(decrement)操作. 2.list 一个链表, ...
- jupyter notebook 安装代码提示功能
我的是在anaconda中的root环境下运行以下命令,在其他环境下发现没有效果 1 pip install jupyter_contrib_nbextensions jupyter contrib ...
- 全国计算机等级考试二级教程2019年版——Python语言程序设计参考答案
第二章 Python语言基本语法元素 一.选择题C B B C A D B A D B二.编程题1.获得用户输入的一个整数N,计算并输出N的32次方.在这里插入图片描述2.获得用户输入的一段文字,将这 ...
- 通过Vagrant搭建PHP环境(一) Vagrant box添加配置
系统Windows10 Vagrant 1.8.1 VirtualBox 5.0.20 vagrant box下载地址:http://cloud.centos.org/centos/7/vagrant ...
- <JavaScript> 寄生继承详解
// 将原型继承和非原型继承组合为一体的继承方式叫做组合继承,但是这种方法的继承是有一点小缺陷的,下级函数继承了无用的属性,所以我们有了寄生继承来解决污染问题; //创建上级构造函数-食物 funct ...
- spring 事务-使用@Transactional 注解(事务隔离级别)
转: spring 事务-使用@Transactional 注解(事务隔离级别) 2016年08月11日 21:49:20 华华鱼 阅读数 15490 标签: spring事务Transactiona ...
- SpringBoot学习之一 Unable to find a single main class from the following candidates
在启动SpringBoot项目是报错 Unable to find a single main class from the following candidates [boot.myboot.Sam ...
- labelme
项目:https://github.com/wkentaro/labelme?tdsourcetag=s_pcqq_aiomsg 说明:https://www.bilibili.com/video/a ...
- PAT 甲级 1015 Reversible Primes (20 分) (进制转换和素数判断(错因为忘了=))
1015 Reversible Primes (20 分) A reversible prime in any number system is a prime whose "rever ...
- [Graphics] UIColor created with component values far outside the expected range, Set a breakpoint on UIColorBreakForOutOfRangeColorComponents to debug. This message will only be logged once.
用了别人的代码,一直总有一个报错,一开始没注意,最近项目快完期了,得处理下警告之类的东西, 后面发现之前那个大神代码是这样写的 [SVProgressHUD setBackgroundColor:[U ...