Linux环境编程之同步(二):条件变量
相互排斥锁用于上锁,条件变量则用于等待。条件变量是类型为pthread_cond_t的变量。一般使用例如以下函数:
#include <pthread.h>
int pthread_cond_wait(pthread_cond_t *cptr, pthread_mutex_t *mptr);
int pthread_cond_signal(pthread_cond_t *cptr);
每一个条件变量总是有一个相互排斥锁与之关联。调用pthread_cond_wait等待某个条件为真时,还会指定其条件变量的地址和所关联的相互排斥锁的地址。
使用条件变量的生产者-消费者程序例如以下:
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <pthread.h> #define MAXNITEMS 1000000
#define MAXNTHREADS 100 /*globals shared by threads*/
int nitems; /*read-only by producer and consumer*/
int buff[MAXNITEMS];
struct{
pthread_mutex_t mutex;
int nput; /*next index to store*/
int nval; /*next value to store*/
}put = {
PTHREAD_MUTEX_INITIALIZER
}; struct{
pthread_mutex_t mutex;
pthread_cond_t cond;
int nready; /*number ready for consumer*/
}nready={
PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER
}; int min(int a, int b)
{
return (a < b) ? (a) : (b);
} void *produce(void *), *consume(void *); int
main(int argc, char **argv)
{
int i, nthreads, count[MAXNTHREADS];
pthread_t tid_produce[MAXNTHREADS], tid_consume; if(argc != 3){
printf("usage:produces6 <#items> <#threads>.\n");
return -1;
}
nitems = min(atoi(argv[1]), MAXNITEMS);
nthreads = min(atoi(argv[2]), MAXNTHREADS); /*create all producers and one consumer*/
pthread_setconcurrency(nthreads + 1);
for(i = 0; i < nthreads; i++){
count[i] = 0;
pthread_create(&tid_produce[i], NULL, produce, &count[i]);
}
pthread_create(&tid_consume, NULL, consume, NULL); /*wait for all producers and the consumer*/
for(i = 0; i < nthreads; i++){
pthread_join(tid_produce[i], NULL);
printf("count[%d] = %d\n", i, count[i]);
}
pthread_join(tid_consume, NULL); exit(0);
} void *
produce(void *arg)
{
for(;;){
pthread_mutex_lock(&put.mutex);
if(put.nput >= nitems){
pthread_mutex_unlock(&put.mutex);
return (NULL); /*array is full, we're done*/
}
buff[put.nput] = put.nval;
put.nput++;
put.nval++;
pthread_mutex_unlock(&put.mutex); pthread_mutex_lock(&nready.mutex);
if(nready.nready == 0){
pthread_cond_signal(&nready.cond);
}
nready.nready++;
pthread_mutex_unlock(&nready.mutex); *((int *)arg) += 1;
}
} void *
consume(void *arg)
{
int i;
for(i = 0; i < nitems; i++){
pthread_mutex_lock(&nready.mutex);
while(nready.nready == 0)
pthread_cond_wait(&nready.cond, &nready.mutex);
nready.nready--;
pthread_mutex_unlock(&nready.mutex); if(buff[i] != i)
printf("buff[%d] = %d\n", i, buff[i]);
}
return(NULL);
}
Linux环境编程之同步(二):条件变量的更多相关文章
- UNIX环境高级编程——线程同步之条件变量以及属性
条件变量变量也是出自POSIX线程标准,另一种线程同步机制.主要用来等待某个条件的发生.可以用来同步同一进程中的各个线程.当然如果一个条件变量存放在多个进程共享的某个内存区中,那么还可以通过条件变量来 ...
- Linux环境编程之同步(四):Posix信号量
信号量是一种用于提供不同进程间或一个给定进程的不同线程间同步手段的原语.有三种类型:Posix有名信号量,使用Posix IPC名字标识.Posix基于内存的信号量,存放在共享内存区中:System ...
- Linux环境编程之同步(三):读写锁
概述 相互排斥锁把试图进入我们称之为临界区的全部其它线程都堵塞住.该临界区通常涉及对由这些线程共享一个或多个数据的訪问或更新.读写锁在获取读写锁用于读某个数据和获取读写锁用于写直接作差别. 读写锁的分 ...
- Linux环境编程相关的文章
Linux环境编程相关的文章 好几年没有接触Linux环境下编程了,好多东西都有点生疏了.趁着现在有空打算把相关的一些技能重拾一下,顺手写一些相关的文章加深印象. 因为不是写书,也受到许多外部因素限制 ...
- Linux同步机制(二) - 条件变量,信号量,文件锁,栅栏
1 条件变量 条件变量是一种同步机制,允许线程挂起,直到共享数据上的某些条件得到满足. 1.1 相关函数 #include <pthread.h> pthread_cond_t cond ...
- 四十二、Linux 线程——线程同步之条件变量之线程状态转换
42.1 线程状态转换 42.1.1 状态转换图 42.1.2 一个线程计算,多个线程获取的案例 #include <stdio.h> #include <stdlib.h> ...
- (转)Linux C 多线程编程----互斥锁与条件变量
转:http://blog.csdn.net/xing_hao/article/details/6626223 一.互斥锁 互斥量从本质上说就是一把锁, 提供对共享资源的保护访问. 1. 初始化: 在 ...
- linux多线程同步pthread_cond_XXX条件变量的理解
在linux多线程编程中,线程的执行顺序是不可预知的,但是有时候由于某些需求,需要多个线程在启动时按照一定的顺序执行,虽然可以使用一些比较简陋的做法,例如:如果有3个线程 ABC,要求执行顺序是A-- ...
- Linux线程同步:条件变量
条件变量通过允许线程阻塞和等待另一个线程发送信号的方法弥补了互斥锁的不足,它常和互斥锁一起使用.使用时,条件变量被用来阻塞一个线程,当条件不满足时,线程往往解开相应的互斥锁并等待条件发生变化.一旦其它 ...
随机推荐
- 用c#实现单链表(程序代码已经验证,完全正确)
1.程序的大致结构如下图: 2.下面依次列出各个类的代码 ①ILISTDs.cs 这是一个接口类,列出单链表的方法 using System; using System.Collections.Ge ...
- POJ 2632 Crashing Robots (坑爹的模拟题)
Crashing Robots Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6599 Accepted: 2854 D ...
- 开发指南专题八:JEECG微云高速开发平台数据字典
开发指南专题八:JEECG微云高速开发平台数据字典的使用 1.标签中使用数据字典 数据字典为系统中可能用到的字典类型数据提供了使用的便利性和可维护性.下面拉框标签<t:dictSele ...
- Android使用SharedPreferences保存数组
核心原理: 对象序列化 步骤 1.要保存的对象实现序列化Serializable 2.将序列化的对象保存String(本文的做法是保存为byte数组在转为16进制的String类型保存起来) 3.将保 ...
- UVa11183 Teen Girl Squad, 最小树形图,朱刘算法
Teen Girl Squad Input: Standard Input Output: Standard Output You are part of a group of n teenage ...
- C++(MFC)中WebBrowser去除3D边框的方法(实现IDocHostUIHandler接口)
先说实在的:最终解决办法是实现IDocHostUIHandler接口,在GetHostInfo方法里解决,但“实现接口”意味着QueryInterface.AddRef.Release三个方法必须实现 ...
- 外国的Delphi网站
www.phidels.com delphifr.com http://www.swissdelphicenter.com/torry/showcode.php?id=787 B4A delphifa ...
- qt+boost::asio+tcp文件传输
客户端: void qt_boost::pbSendFileClicked(){ QString filename = ui.leFileName->text(); QByteArray ba ...
- WiPlug_百度百科
WiPlug_百度百科 WiPlug
- 从Hadoop骨架MapReduce在海量数据处理模式(包括淘宝技术架构)
从hadoop框架与MapReduce模式中谈海量数据处理 前言 几周前,当我最初听到,以致后来初次接触Hadoop与MapReduce这两个东西,我便稍显兴奋,认为它们非常是神奇.而神奇的东西常能勾 ...