The
pthread_cond_wait() and
pthread_cond_timedwait() functions are used to block on a condition variable. They are called with
mutex locked by the calling thread or undefined behaviour will result.
These functions atomically release mutex and cause the calling thread to block on the condition variable cond; atomically here means "atomically with respect to access by another thread to the mutex and then the condition variable". That is, if another thread is able to acquire the mutex after the about-to-block thread has released it, then a subsequent call to or in that thread behaves as if it were issued after the about-to-block thread has blocked.
文字解释如下:
pthread_mutex_lock: 获得锁则锁定然后执行第二句话,否则阻塞等锁。
pthread_cond_wait :1.首先解锁相当于pthread_mutex_unlock。2.然后建立锁与条件变量的联系,3.等待唤醒,4.唤醒后第一件事情是上锁相当于pthread_mutex_lock,然后执行下一句话。
生产者--消费者编程模型
-
#include <stdio.h>
-
#include <stdlib.h>
-
#include <unistd.h>
-
#include <pthread.h>
-
-
#define BUFF_SIZE 8
-
-
typedef struct {
-
int buff[BUFF_SIZE];
-
pthread_mutex_t locker;
-
pthread_cond_t notEmpty;
-
pthread_cond_t notFull;
-
int posReadFrom, posWriteTo;
-
} Products;
-
-
int buffer_is_full(Products* product) {
-
if ((product->posWriteTo + 1) % BUFF_SIZE == product->posReadFrom) return 1;
-
return 0;
-
}
-
-
int buffer_is_empty(Products* product) {
-
if (product->posReadFrom == product->posWriteTo) return 1;
-
return 0;
-
}
-
-
void produce(Products* product, int item)
-
{
-
pthread_mutex_lock(&product->locker);
-
-
while (buffer_is_full(product)) {
-
pthread_cond_wait(&product->notFull, &product->locker);
-
}
-
-
product->buff[product->posWriteTo++] = item;
-
if (product->posWriteTo >= BUFF_SIZE)
-
product->posWriteTo = 0;
-
-
// 发信号--not empty
-
pthread_cond_signal(&product->notEmpty);
-
pthread_mutex_unlock(&product->locker);
-
}
-
-
int consume(Products* product)
-
{
-
int item;
-
pthread_mutex_lock(&product->locker);
-
-
while (buffer_is_empty(product)) {
-
pthread_cond_wait(&product->notEmpty, &product->locker);
-
}
-
-
item = product->buff[product->posReadFrom++];
-
if (product->posReadFrom >= BUFF_SIZE)
-
product->posReadFrom = 0;
-
-
// 发信号--not full
-
pthread_cond_signal(&product->notFull);
-
pthread_mutex_unlock(&product->locker);
-
-
return item;
-
}
-
-
-
-
#define END_FLAG -1
-
Products gproduce;
-
-
void* producer_thread(void* arg)
-
{
-
for (int i = 0; i < 16; i++) {
-
printf("producer:%d\n", i);
-
produce(&gproduce, i);
-
usleep(1000*100);
-
}
-
-
produce(&gproduce, END_FLAG);
-
return NULL;
-
}
-
-
void* consumer_thread(void* arg)
-
{
-
while (1) {
-
int item = consume(&gproduce);
-
if (END_FLAG == item) break;
-
printf("consumer:%d\n", item);
-
}
-
-
return NULL;
-
}
-
-
int main(int argc, char* argv[])
-
{
-
pthread_t producer_id, consumer_id;
-
int result;
-
-
pthread_create(&producer_id, NULL, producer_thread, NULL);
-
pthread_create(&consumer_id, NULL, consumer_thread, NULL);
-
-
pthread_join(producer_id, NULL);
-
pthread_join(consumer_id, NULL);
-
-
return 0;
-
}
附代码及编译文件
pthreadtest.rar
阅读(1502) | 评论(0) | 转发(0) |