条件锁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的更多相关文章

  1. 条件变量pthread_cond_t怎么用

    #include <pthread.h> #include <stdio.h> #include <stdlib.h> pthread_mutex_t mutex ...

  2. 通用线程:POSIX 线程详解,第 3 部分 条件互斥量(pthread_cond_t)

    使用条件变量提高效率 本文是 POSIX 线程三部曲系列的最后一部分,Daniel 将详细讨论如何使用条件变量.条件变量是 POSIX 线程结构,可以让您在遇到某些条件时“唤醒”线程.可以将它们看作是 ...

  3. Linux多线程编程详细解析----条件变量 pthread_cond_t

    Linux操作系统下的多线程编程详细解析----条件变量 1.初始化条件变量pthread_cond_init #include <pthread.h> int pthread_cond_ ...

  4. 关于一点pthread_cond_t条件锁的思考以及实验

    转:http://blog.csdn.net/aniao/article/details/5802015 APUE上,关于条件锁.其中有这么几条总结: 1.使用条件锁前必须先锁住对应的互斥锁. 2.条 ...

  5. ZT 为什么pthread_cond_t要和pthread_mutex_t同时使用 || pthread/Linux多线程编程

    为什么线程同步的时候pthread_cond_t要和pthread_mutex_t同时使用 (2009-10-27 11:07:23) 转载▼ 标签: 杂谈 分类: 计算机 举一个例子(http:// ...

  6. POSIX多线程编程-条件变量pthread_cond_t

    条件变量通过允许线程阻塞和等待另一个线程发送信号的方法弥补了互斥锁的不足,它常和互斥锁一起使用.使用时,条件变量被用来阻塞一个线程,当条件不满足时,线程往往解开相应的互斥锁并等待条件发生变化.一旦其它 ...

  7. pthread_mutex_t & pthread_cond_t 总结

    pthread_mutex_t & pthread_cond_t 总结 一.多线程并发 1.1 多线程并发引起的问题 我们先来看如下代码: #include <stdio.h> # ...

  8. int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restric mutex)

    mutex:为了保护条件变量而存在的: cond:为了线程通信而存在的. 整个机制都是为了保护条件变量和线程间通信而存在的. pthread_cond_wait()函数一进入wait状态就会自动rel ...

  9. 《Note --- Unreal --- MemPro (CONTINUE... ...)》

    Mem pro 是一个主要集成内存泄露检测的工具,其具有自身的源码和GUI,在GUI中利用"Launch" button进行加载自己待检测的application,目前支持的平台为 ...

随机推荐

  1. jinja2-模版简介

    一 简介 在Flask中,调用render_template来对模版进行渲染,使用render_template,只需要导入这个API就可以,from flask import render_temp ...

  2. chrome jssip

    WebRTC 实现了基于网页的视频会议,标准是WHATWG 协议,目的是通过浏览器提供简单的javascript就可以达到实时通讯(Real-Time Communications (RTC))能力 ...

  3. 一个数据库操作类,适用于Oracle,ACCESS,SQLSERVER

    最近做了一个数据诊断的项目,里面自己写了一个数据库的操作类,包含:连接数据库.读数据表.执行SQL操作,释放数据库等组成,希望对大家有用,由于水平有限,若有错误或者代码不足地方欢迎指正,谢谢. ADO ...

  4. Flutter移动电商实战 --(26)列表页_使用Provide控制子类-2

    主要实现功能,点击一级分类,二级分类跟着变.这里主要用我们的provide 新建provide provide文件夹下创建:child_category.dart 事件上就是这个实体:BxMallSu ...

  5. vue2.0+vue-dplayer实现hls播放

    vue2.0+vue-dplayer实现hls播放 开始 安装依赖 npm install vue-dplayer -S 1,编写组件HelloWorld.vue <template> & ...

  6. kotlin中匿名对象

    open class MyClass { private fun too()=object { var x : String ="x" } fun publictoo()=obje ...

  7. kotlin 类的继承

    与Java不同,kotlin 使用冒号,而Java 中使用extends, 注意冒号后面需要调用夫类的构造器.属于单继承,使用open 关键字允许继承class package loaderman.d ...

  8. Linux -- 进程或线程独占CPU

    如果想让特定进程或线程独占某一或某些CPU,我们需要做三件事. 一,隔离CPU,避免其它线程run在被隔离的CPU上. 二,绑定所有的interrupts到非隔离的CPU上,避免被隔离的CPU收到in ...

  9. js 匿名函数 js-函数定义方法

    1.任何函数都是有返回值的,没有返回值的,在某些语言里称之为过程例如PL/SQL 2.js中的函数如果没有return 关键字指明给出的返回值,那么当调用完函数后,会返回“undefined" ...

  10. Centos7 系统更改apache默认网站目录(解决You don't have permission to access / on this server问题)

    当我们在Centos7中配置好Apache时,发现apache默认解析目录是在 /var/www/html,也就是说当访问服务器 IP 或者本地 localhost 时, 默认定位到这个目录里的 in ...