概念:条件变量不是锁,要和互斥量组合使用。条件变量就是生产者“生产”完成,消费者才能“使用”,如果没有“产品”,消费者就会被条件变量cond阻塞等待生产者“生产”。(生产者与消费者模型)
函数:
int pthread_cond_timedwait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex,
const struct timespec *restrict abstime);
int pthread_cond_wait(pthread_cond_t *restrict cond, pthread_mutex_t *restrict mutex);
struct timespec{
time_t tv_sec; /*seconds*/秒
long tv_nsec; /*nanoseconds*/纳秒
}
tv_sec 绝对时间,填写的时候,例如time(NULL)+600,表示设置超时600s。
int pthread_cond_wait(pthread_cond_t *restrict cond,
pthread_mutex_t *restrict mutex);
先释放锁mutex;
阻塞在cond条件变量上。
int pthread_cond_init(pthread_cond_t *restrict cond,
const pthread_condattr_t *restrict attr);
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
int pthread_cond_destroy(pthread_cond_t *cond);
- 5.信号,唤醒至少一个阻塞在条件变量cond上的线程
int pthread_cond_signal(pthread_cond_t *cond);
int pthread_cond_broadcast(pthread_cond_t *cond);
示例:创建两个线程,一个线程为生产者,两个线程为消费者。(生产者与消费者模型)
#include<stdio.h>
#include<unistd.h>
#include<pthread.h>
#include<stdlib.h>
int beginnum = 1000;
//初始化互斥锁和条件变量
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_cond_t cond = PTHREAD_COND_INITIALIZER;
//定义一个链表,用于存取数据
typedef struct _ProdInfo
{
int num;
struct _ProdInfo *next;
}ProdInfo;
ProdInfo *Head = NULL;
void *thr_producter(void *arg)
{
//负责在链表中添加数据
while (1)
{
ProdInfo * prod = malloc(sizeof(ProdInfo));
prod->num = beginnum++;
printf("----%s----self = %lu----%d\n",__FUNCTION__,pthread_self(),prod->num);
pthread_mutex_lock(&mutex);
//添加到链表
prod->next = Head;
Head = prod;
pthread_mutex_unlock(&mutex);
//发起通知
pthread_cond_signal(&cond);
sleep(rand()%2);
//free(prod); //重复释放内存会报错,用完在释放
}
return NULL;
}
void *thr_customer(void *arg)
{
ProdInfo *prod = NULL;
while (1)
{
//获取链表中的数据
pthread_mutex_lock(&mutex);
//if(Head = NULL)
while (Head == NULL)
{
pthread_cond_wait(&cond,&mutex); //在此之前必须加锁
}
prod = Head;
Head = Head->next;
printf("----%s----self = %lu----%d\n",__FUNCTION__,pthread_self(),prod->num);
pthread_mutex_unlock(&mutex);
sleep(rand()%4);
free(prod);
}
return NULL;
}
int main()
{
pthread_t tid[3];
pthread_create(&tid[0],NULL,thr_producter,NULL);
pthread_create(&tid[1],NULL,thr_customer,NULL);
pthread_create(&tid[2],NULL,thr_customer,NULL);
pthread_join(tid[0],NULL);
pthread_join(tid[1],NULL);
pthread_join(tid[2],NULL);
pthread_mutex_destroy(&mutex);
pthread_mutex_destroy(&cond);
return 0;
}
输出:
~$ gcc cond_product.c -lpthread
~$ ./a.out
----thr_producter----self = 140197865371392----1000
----thr_customer----self = 140197856978688----1000
----thr_producter----self = 140197865371392----1001
----thr_customer----self = 140197731759872----1001
----thr_producter----self = 140197865371392----1002
----thr_customer----self = 140197856978688----1002
----thr_producter----self = 140197865371392----1003
----thr_producter----self = 140197865371392----1004
----thr_producter----self = 140197865371392----1005
----thr_customer----self = 140197731759872----1005
----thr_producter----self = 140197865371392----1006
----thr_producter----self = 140197865371392----1007
----thr_customer----self = 140197856978688----1007
----thr_customer----self = 140197731759872----1006
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)