pthread_cond_init 函数原型:
pthread_cond_init 初始化条件变量 | ||
所需头文件 | #include <pthread.h> | |
函数说明 | 初始化一个条件变量 | |
函数原型 |
int pthread_cond_init(pthread_cond_t *restrict cond, const pthread_condattr_t *restrict attr) |
函数传入值
cond
指向要初始化的条件变量的指针
attr
当参数attr为空指针时,函数创建的是一个缺省的条件变量。否则条件变量的属性将由attr中的属性值来决定。
函数返回值
成功:0
出错:返回错误码
错误代码
EAGAIN:系统缺少足够的资源去初始化其他的互斥量
ENOMEM:内存不足去初始化条件变量
EBUSY:实施检测尝试初始化cond所引用的对象,以前初始化,但尚未销毁的,条件变量。
pthread_cond_destroy 函数原型:
pthread_cond_destroy 释放条件变量 | ||
所需头文件 | #include <pthread.h> | |
函数说明 | 释放条件变量 | |
函数原型 | int pthread_cond_destroy(pthread_cont_t *cond) | |
函数传入值 | cont | 要释放的条件变量的参数 |
| ||
| ||
| ||
函数返回值 | 成功:返回0 | |
失败:返回错误码 | ||
错误代码 | EBUSY:检测到企图摧毁被锁定的通过cond属性引用的目标 或者 被另一线程引用
EINVAL: cond所指定的值失效
|
pthread_cond_signal函数原型:
pthread_cond_signal 发送一个条件变量 | ||
所需头文件 | #include <pthread.h> | |
函数说明 | 用来释放被阻塞在指定条件变量上的一个线程。 | |
函数原型 | int pthread_cond_signal(pthread_cont_t *cond) | |
函数传入值 | cont | 要释放的条件变量的参数 |
| ||
| ||
| ||
函数返回值 | 成功:返回0 | |
失败:返回错误码 | ||
错误代码 | EINVAL: cond值并不是参照一个初始化条件变量 |
pthread_cond_ broadcast函数原型:
pthread_cond_broadcast广播一个条件变量 | ||
所需头文件 | #include <pthread.h> | |
函数说明 | 释放阻塞的所有线程 | |
函数原型 | int pthread_cond_broadcast(pthread_cont_t *cond) | |
函数传入值 | cont | 要释放的条件变量的参数 |
| ||
| ||
| ||
函数返回值 | 成功:返回0 | |
失败:返回错误码 | ||
错误代码 | EINVAL: cond值并不是参照一个初始化条件变量 |
pthread_cond_ wait函数原型:
pthread_cond_wait 阻塞当前线程 | ||
所需头文件 | #include <pthread.h> | |
函数说明 | 用于阻塞当前线程 | |
函数原型 | int pthread_cond_wait(pthread_cont_t *cond | |
函数传入值 | cond |
指定堵塞的条件变量参数 |
mutex
指定堵塞的互斥量
函数返回值
成功:返回0
失败:返回错误码
错误代码
EINVAL:cond,mutex,abstime所指定的值无效
EINVAL:不同的互斥量在相同的条件变量下为pthread_cond_timedwait或者
pthread_cond_wait函数提供并发操作
EPERM:没有由当前线程被调用时的互斥量
pthread_cond_ timedwait 创建一个新线程 | ||
所需头文件 | #include <pthread.h> | |
函数说明 | 在函数调用时创建一个新线程 | |
函数原型 | int pthread_timedwait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex, const struct timespec *restrict abstime); | |
函数传入值 | cond | 指定堵塞的条件变量参数 |
mutex | 指定堵塞的互斥量 | |
abstime | 指定堵塞的绝对时间 | |
| | |
| ||
| ||
函数返回值 | 成功:0 | |
错误:返回错误码 | ||
错误代码 | ETIMEDOUT:abstime指定的时间在pthread_timedwait中已经执行完毕
EINVAL:cond,mutex,abstime所指定的值无效 pthread_cond_wait函数提供并发操作
|
详细解释请看:
http://blog.csdn.net/hudashi/article/details/7709421
实例一:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pthread.h>
#define BUFFER_SIZE 8
struct Products
{
int buffer[BUFFER_SIZE];
/*Atomicity mutually for guaranteed access operations */
pthread_mutex_t locker;
/*Is readable*/
pthread_cond_t notEmpty;
/*Is writable*/
pthread_cond_t notFull;
int posReadFrom;
int posWriteTo;
};
/*Buffer is full*/
int BufferIsFull(struct Products* products)
{
if((products->posWriteTo + 1)%BUFFER_SIZE == products->posReadFrom)
{
return 1;
}
return 0;
}
/*Buffer is empty*/
int BufferIsEmpty(struct Products* products)
{
if((products->posWriteTo)%BUFFER_SIZE == products->posReadFrom)
{
return 1;
}
return 0;
}
/*manufactured products*/
void Produce(struct Products *products,int item)
{
/*Atomic operation*/
pthread_mutex_lock(&products->locker);
while(BufferIsFull(products))
{
pthread_cond_wait(&products->notFull,&products->locker);
}
/*write data*/
products->buffer[products->posWriteTo] = item;
products->posWriteTo++;
if(products->posWriteTo >= BUFFER_SIZE);
{
products->posWriteTo = 0;
}
/*send signal*/
pthread_cond_signal(&products->notEmpty);
printf("item: %d\n",item);
// sleep(1);
/*unlock*/
pthread_mutex_unlock(&products->locker);
}
/*consume*/
int Consume(struct Products *products)
{
int item;
pthread_mutex_lock(&products->locker);
/*waiting for time,not data*/
while(BufferIsEmpty(products))
{
pthread_cond_wait(&products->notEmpty,&products->locker);
}
/*Extracting data*/
item = products->buffer[products->posReadFrom];
products->posReadFrom++;
/*If read the end ,then from the beginning*/
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);
// sleep(1);
}
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);
sleep(2);
pthread_join(producer,(void *)&result);
pthread_join(consumer,(void *)&result);
exit(EXIT_SUCCESS);
}
实例二:
#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(&mymutex1);
printf("wait in mythread2.\n");
pthread_cond_wait(&mycond,&mymutex1);
pthread_mutex_unlock(&mymutex1);
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(2);
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;
}