线程互斥锁和条件变量应用
来源:互联网 发布:淘宝话费慢充 编辑:程序博客网 时间:2024/04/30 04:24
在程序当中,创建两个线程,其中一个为生产者,一个为消费者。同时创建一把互斥锁保证两者不能够同时访问结构体当中buffer,可以把它比作是一个“仓库”。必须要等生产完成之后才可以去采用读取。添加条件变量是与锁综合起来的条件判定。具体程序如下..
#include <stdlib.h>
#include <time.h>
#include "pthread.h"
#define BUFFER_SIZE 2
/* Circular buffer of integers. */
struct prodcons
{
int buffer[BUFFER_SIZE]; /* the actual data */
pthread_mutex_t lock; /* mutex ensuring exclusive access to buffer */
int readpos, writepos; /* positions for reading and writing */
pthread_cond_t notempty; /* signaled when buffer is not empty */
pthread_cond_t notfull; /* signaled when buffer is not full */
};
/* Initialize a buffer */
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;
}
/* Store an integer in the buffer */
void put(struct prodcons * prod, int data)
{
pthread_mutex_lock(&prod->lock);
/* Wait until buffer is not full */
while ((prod->writepos + 1) % BUFFER_SIZE == prod->readpos)
{
printf("producer wait for not full\n");
pthread_cond_wait(&prod->notfull, &prod->lock);
}
/* Write the data and advance write pointer */
prod->buffer[prod->writepos] = data;
prod->writepos++;
if (prod->writepos >= BUFFER_SIZE)
prod->writepos = 0;
/*Signal that the buffer is now not empty */
pthread_cond_signal(&prod->notempty);
pthread_mutex_unlock(&prod->lock);
}
/* Read and remove an integer from the buffer */
int get(struct prodcons *prod)
{
int data;
pthread_mutex_lock(&prod->lock);
/* Wait until buffer is not empty */
while (prod->writepos == prod->readpos)
{
printf("consumer wait for not empty\n");
pthread_cond_wait(&prod->notempty, &prod->lock);
}
/* Read the data and advance read pointer */
data = prod->buffer[prod->readpos];
prod->readpos++;
if (prod->readpos >= BUFFER_SIZE)
prod->readpos = 0;
/* Signal that the buffer is now not full */
pthread_cond_signal(&prod->notfull);
pthread_mutex_unlock(&prod->lock);
return data;
}
#define OVER (-1)
struct prodcons buffer;
/*--------------------------------------------------------*/
void * producer(void * data)
{
int n;
for (n = 0; n < 5; n++)
{
printf("producer sleep 1 second......\n");
sleep(1);
printf("put the %d product\n", n);
put(&buffer, n);
}
for(n=5; n<10; n++)
{
printf("producer sleep 3 second......\n");
sleep(3);
printf("put the %d product\n",n);
put(&buffer,n);
}
put(&buffer, 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(&buffer);
printf("get the %d product\n", d);
// d = get(&buffer);
if (d == OVER ) break;
}
printf("consumer stopped!\n");
return NULL;
}
/*--------------------------------------------------------*/
int main(int argc,char *argv[])
{
pthread_t th_a, th_b;
void * retval;
init(&buffer);
pthread_create(&th_a, NULL, producer, 0);
pthread_create(&th_b, NULL, consumer, 0);
/* Wait until producer and consumer finish. */
pthread_join(th_a, &retval);
pthread_join(th_b, &retval);
return 0;
}
- 线程互斥锁和条件变量应用
- 线程条件变量应用(消费者和生产着模型)
- [C/C++]_[初级]_[ 线程pthread学习之互斥锁和条件变量的应用 ]
- 线程条件变量pthread_cond_t和线程条件锁详解
- 线程间同步实现--通过互斥锁和条件变量
- 线程同步之互斥锁、读写锁和条件变量
- Linux下线程同步问题中的互斥锁和条件变量
- 线程同步的方法—互斥锁、信号量和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 条件变量和互斥锁
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- 互斥锁和条件变量
- [部署Mantis]用Administrator注册新用户时设置密码
- ado连接字符串总结
- ios 常见系统控件尺寸
- Linux-2.6.20的cs8900驱动分析
- Javascript 中的false,零值,null,undefined和空字符串对象
- 线程互斥锁和条件变量应用
- 使用Umdh对付内存泄漏
- GetSafeHwnd()和GetSafeHandle()的主要区别:
- 基于TCP的socket编程设计
- Boost::asio io_service 实现分析
- Java Swing系统托盘的使用
- lua 1.0 源代码的类图
- 备忘录模式
- 视频读取速度太快