linux编程---线程---条件变量
2017-08-23 15:00
239 查看
条件变量通信机制
基本原理
初始化条件变量
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);
通知等待条件变量的线程
用于唤醒等待出现与条件变量cond关联的条件的所有线程
int pthread_cond_broadcast(pthread_cond_t *cond);
用于唤醒等待出现与条件变量cond关联的条件的第一条线程
int pthread_cond_signal(pthread_cond_t *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);
如果某线程因等待条件变量进入等待状态时,将隐含释放其申请的互斥锁。
同样,在返回时,首先要申请到该互斥锁对象。
程序示例:
程序处理生产消费问题,整个临时存储空间为2,即在任意时刻,最多能够有2个产品存放在临时
空间,如果已经有2个产品存放在临时空间,将阻塞生产线程。如果临时空间中没有产品,显示
需要阻塞消费线程。程序主要实现了生产和消费两个线程同步。
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<time.h>
#include<pthread.h>
#define BUFSIZE 2
struct prodcons
{
int buf[BUFSIZE];
pthread_mutex_t lock;
int readpos,writepos;
pthread_cond_t notempty;
pthread_cond_t notfull;
};
void init(struct prodcons *prod)
{
pthread_mutex_init(&prod->lock,NULL);
pthread_cond_init(&prod->notempty,NULL);
pthread_cond_init(&prod->notfull,NULL);
prod->readpos = 0;
prod->writepos = 0;
}
void put(struct prodcons* prod,int data)
{
pthread_mutex_lock(&prod->lock);
while((prod->writepos+1) % BUFSIZE == prod->readpos)
{
printf("producer wait for not full\n");
pthread_cond_wait(&prod->notfull,&prod->lock);
}
prod->buf[prod->writepos] = data;
prod->writepos++;
if(prod->writepos >= BUFSIZE)
prod->writepos = 0;
pthread_cond_signal(&prod->notempty);
pthread_mutex_unlock(&prod->lock);
}
int get(struct prodcons* prod)
{
int data;
pthread_mutex_lock(&prod->lock);
while(prod->writepos == prod->readpos)
{
printf("consumer wait for not empty\n");
pthread_cond_wait(&prod->notempty,&prod->lock);
}
data = prod->buf[prod->readpos];
prod->readpos++;
if(prod->readpos >= BUFSIZE)
prod->readpos = 0;
pthread_cond_signal(&prod->notfull);
pthread_mutex_unlock(&prod->lock);
return data;
}
#define OVER (-1)
struct prodcons buf;
void* producer(void* data)
{
int n;
for(n=1;n <= 5;n++)
{
printf("producer sleep 1 second....\n");
sleep(1);
printf("put the %d producet\n",n);
put(&buf,n);
}
for(n=6;n <= 10;n++)
{
printf("producer sleep 3 second....\n");
sleep(3);
printf("put the %d producet\n",n);
put(&buf,n);
}
put(&buf,OVER);
printf("producer stopped\n");
return NULL;
}
void* consumer(void* data)
{
int d =0;
while(1)
{
printf("consumer sleep 2 second...\n");
sleep(2);
d = get(&buf);
printf("get the %d product\n",d);
if(d == OVER)
break;
}
printf("consumer stopped\n");
return NULL;
}
int main()
{
pthread_t th1,th2;
void* retval;
init(&buf);
pthread_create(&th1,NULL,producer,0);
pthread_create(&th2,NULL,consumer,0);
pthread_join(th1,&retval);
pthread_join(th2,&retval);
return 0;
}
基本原理
初始化条件变量
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);
通知等待条件变量的线程
用于唤醒等待出现与条件变量cond关联的条件的所有线程
int pthread_cond_broadcast(pthread_cond_t *cond);
用于唤醒等待出现与条件变量cond关联的条件的第一条线程
int pthread_cond_signal(pthread_cond_t *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);
如果某线程因等待条件变量进入等待状态时,将隐含释放其申请的互斥锁。
同样,在返回时,首先要申请到该互斥锁对象。
程序示例:
程序处理生产消费问题,整个临时存储空间为2,即在任意时刻,最多能够有2个产品存放在临时
空间,如果已经有2个产品存放在临时空间,将阻塞生产线程。如果临时空间中没有产品,显示
需要阻塞消费线程。程序主要实现了生产和消费两个线程同步。
#include<stdio.h>
#include<unistd.h>
#include<stdlib.h>
#include<time.h>
#include<pthread.h>
#define BUFSIZE 2
struct prodcons
{
int buf[BUFSIZE];
pthread_mutex_t lock;
int readpos,writepos;
pthread_cond_t notempty;
pthread_cond_t notfull;
};
void init(struct prodcons *prod)
{
pthread_mutex_init(&prod->lock,NULL);
pthread_cond_init(&prod->notempty,NULL);
pthread_cond_init(&prod->notfull,NULL);
prod->readpos = 0;
prod->writepos = 0;
}
void put(struct prodcons* prod,int data)
{
pthread_mutex_lock(&prod->lock);
while((prod->writepos+1) % BUFSIZE == prod->readpos)
{
printf("producer wait for not full\n");
pthread_cond_wait(&prod->notfull,&prod->lock);
}
prod->buf[prod->writepos] = data;
prod->writepos++;
if(prod->writepos >= BUFSIZE)
prod->writepos = 0;
pthread_cond_signal(&prod->notempty);
pthread_mutex_unlock(&prod->lock);
}
int get(struct prodcons* prod)
{
int data;
pthread_mutex_lock(&prod->lock);
while(prod->writepos == prod->readpos)
{
printf("consumer wait for not empty\n");
pthread_cond_wait(&prod->notempty,&prod->lock);
}
data = prod->buf[prod->readpos];
prod->readpos++;
if(prod->readpos >= BUFSIZE)
prod->readpos = 0;
pthread_cond_signal(&prod->notfull);
pthread_mutex_unlock(&prod->lock);
return data;
}
#define OVER (-1)
struct prodcons buf;
void* producer(void* data)
{
int n;
for(n=1;n <= 5;n++)
{
printf("producer sleep 1 second....\n");
sleep(1);
printf("put the %d producet\n",n);
put(&buf,n);
}
for(n=6;n <= 10;n++)
{
printf("producer sleep 3 second....\n");
sleep(3);
printf("put the %d producet\n",n);
put(&buf,n);
}
put(&buf,OVER);
printf("producer stopped\n");
return NULL;
}
void* consumer(void* data)
{
int d =0;
while(1)
{
printf("consumer sleep 2 second...\n");
sleep(2);
d = get(&buf);
printf("get the %d product\n",d);
if(d == OVER)
break;
}
printf("consumer stopped\n");
return NULL;
}
int main()
{
pthread_t th1,th2;
void* retval;
init(&buf);
pthread_create(&th1,NULL,producer,0);
pthread_create(&th2,NULL,consumer,0);
pthread_join(th1,&retval);
pthread_join(th2,&retval);
return 0;
}
相关文章推荐
- linux下C编程学习笔记之线程学习(一)
- Linux操作系统下的多线程编程详细解析----条件变量
- Linux下面的线程锁,条件变量以及信号量的使用
- Linux编程---线程
- LInux多线程编程----线程属性pthread_attr_t
- Linux的多任务编程-线程
- Linux 线程锁 条件变量 信号量的区别2
- Linux多线程编程详细解析----条件变量 pthread_cond_t
- linux 线程的同步 二 (互斥锁和条件变量)
- Linux 多线程编程入门--线程函数解释
- 【原创】《Linux高级程序设计》杨宗德著 - Linux多线程编程 - 线程概念及创建线程 分类: Linux --- 应用程序设计 2014-11-19 17:31 82人阅读 评论(0) 收藏
- 【Linux C 多线程编程】互斥锁与条件变量
- posix多线程有感--线程高级编程(条件变量)
- linux 网络编程:使用两线程实现socket同时收发数据
- Linux编程常用的函数(二) 线程
- Linux编程练习 --多线程4--条件变量
- linux编程---线程
- linux编程---线程---读写锁
- 5.Linux应用编程——信号量(POSIX—线程)