标签:style blog color io os 使用 ar strong for
int x,y; pthread_mutex_t mut = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
pthread_mutex_lock(&mut); while (x <= y) { pthread_cond_wait(&cond, &mut); } /* operate on x and y */ pthread_mutex_unlock(&mut);
pthread_mutex_lock(&mut); /* modify x and y */ if (x > y) pthread_cond_broadcast(&cond); pthread_mutex_unlock(&mut);
#include <stdio.h> #include <stdlib.h> #include <unistd.h> #include <pthread.h> #define BUFFER_SIZE 8 struct Products { int buffer[BUFFER_SIZE]; /*保证存取操作的原子性 互斥性*/ pthread_mutex_t locker; /*是否可读*/ pthread_cond_t notEmpty; /*是否可写*/ pthread_cond_t notFull; int posReadFrom; int posWriteTo; }; int BufferIsFull(struct Products* products) { if ((products->posWriteTo + 1) % BUFFER_SIZE == products->posReadFrom) { return (1); } return (0); } int BufferIsEmpty(struct Products* products) { if (products->posWriteTo == products->posReadFrom) { return (1); } return (0); } /*制造产品*/。 void Produce(struct Products* products, int item) { /*原子操作*/ pthread_mutex_lock(&products->locker); /*无空间可写入*/ while (BufferIsFull(products)) { pthread_cond_wait(&products->notFull, &products->locker); } /*写入数据*/ products->buffer[products->posWriteTo] = item; products->posWriteTo++; if (products->posWriteTo >= BUFFER_SIZE) products->posWriteTo = 0; /*发信*/ pthread_cond_signal(&products->notEmpty); /*解锁*/ pthread_mutex_unlock(&products->locker); } int Consume(struct Products* products) { int item; pthread_mutex_lock(&products->locker); /*为空时持续等待,无数据可读*/ while (BufferIsEmpty(products)) { pthread_cond_wait(&products->notEmpty, &products->locker); } /*提取数据*/ item = products->buffer[products->posReadFrom]; products->posReadFrom++; /*如果到末尾,从头读取*/ if (products->posReadFrom >= BUFFER_SIZE) products->posReadFrom = 0; pthread_cond_signal(&products->notFull); pthread_mutex_unlock(&products->locker); return item; } #define END_FLAG (-1) struct Products products; void* ProducerThread(void* data) { int i; for (i = 0; i < 16; ++i) { printf("producer: %d\n", i); Produce(&products, i); } Produce(&products, END_FLAG); return NULL; } void* ConsumerThread(void* data) { int item; while (1) { item = Consume(&products); if (END_FLAG == item) break; printf("consumer: %d\n", item); } return (NULL); } int main(int argc, char* argv[]) { pthread_t producer; pthread_t consumer; int result; pthread_create(&producer, NULL, &ProducerThread, NULL); pthread_create(&consumer, NULL, &ConsumerThread, NULL); pthread_join(producer, (void *)&result); pthread_join(consumer, (void *)&result); exit(EXIT_SUCCESS); }
#include <pthread.h> #include <stdlib.h> #include <stdio.h> #include <unistd.h> #include <string.h> pthread_mutex_t mymutex1 = PTHREAD_MUTEX_INITIALIZER; pthread_mutex_t mymutex2 = PTHREAD_MUTEX_INITIALIZER; pthread_cond_t mycond = PTHREAD_COND_INITIALIZER; void *mythread1(void *param) { printf("begin mythread1.\n"); pthread_mutex_lock(&mymutex1); printf("wait in mythread1.\n"); pthread_cond_wait(&mycond,&mymutex1); pthread_mutex_unlock(&mymutex1); printf("end mythread1.\n"); return NULL; } void *mythread2(void *param) { printf("begin mythread2.\n"); pthread_mutex_lock(&mymutex2); printf("wait in mythread2.\n"); pthread_cond_wait(&mycond,&mymutex2); pthread_mutex_unlock(&mymutex2); printf("end mythread2.\n"); return NULL; } int main(void) { printf("begin main thread.\n"); int i; pthread_t tid1,tid2; pthread_create(&tid1,NULL,mythread1,NULL); pthread_create(&tid2,NULL,mythread2,NULL); sleep(5); printf("try to wake up mythread1 and mythread2 in main thread.\n"); if(pthread_cond_broadcast(&mycond)){ printf("error\n"); return 1; } void *res; pthread_join(tid1, &res); pthread_join(tid2, &res); printf("end main thread.\n"); return 0; }
运行结果:
pthread_cond_signal 和 pthread_cond_wait 简介
标签:style blog color io os 使用 ar strong for
原文地址:http://www.cnblogs.com/eliu/p/3987231.html