条件变量


  
  
#include<semaphore.h> #include<unistd.h> #include<stdio.h> #include<stdlib.h> #include<fcntl.h> #include <sys/mman.h> #include <sys/types.h> #include <sys/stat.h> //#include<linux/msg.h> #include <sys/ipc.h> #include <sys/msg.h> #include<errno.h> #include<string.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 }; /* end globals */ void *produce(void *), *consume(void *); int min(int a,int b) { return a<b?a:b; } /* include main */ int main(int argc, char **argv) { int i, nthreads, count[MAXNTHREADS]; pthread_t tid_produce[MAXNTHREADS], tid_consume; if (argc != 3) printf("usage: prodcons6 <#items> <#threads>"); nitems = min(atoi(argv[1]), MAXNITEMS); nthreads = min(atoi(argv[2]), MAXNTHREADS); pthread_setconcurrency(nthreads + 1); /* 4create all producers and one consumer */ 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); } /* end main */ /* include prodcons */ 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) { printf("nready==0\n"); 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); } /* end prodcons */

定义
Linux下C编程的条件变量:
条件变量是线程中的东西,就是等待某一条件的发生,和信号一样。
用法
条件变量使我们可以睡眠等待某种条件出现。
条件变量是利用线程间共享的全局变量进行同步的一种机制,主要包括两个动作:一个线程等待"条件变量的条件成立"而挂起;另一个线程使"条件成立"(给出条件成立信号)。为了防止竞争,条件变量的使用总是和一个互斥锁结合在一起。
条件变量类型为pthread_cond_t。
创建
条件变量和互斥锁一样,都有静态和动态两种创建方式,静态方式使用PTHREAD_COND_INITIALIZER常量进行初始化,如下:
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
动态方式调用pthread_cond_init()函数,API定义如下:
int pthread_cond_init(pthread_cond_t *cond, pthread_condattr_t *cond_attr)
尽管POSIX标准中为条件变量定义了属性,但在LinuxThreads中没有实现,因此cond_attr值通常为NULL,且被忽略。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值