生产
来源:互联网 发布:mac 列出文件路径 命令 编辑:程序博客网 时间:2024/03/29 07:55
/*****************************************************
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 16
struct prodcons
{
int buffer[BUFFER_SIZE];
pthread_mutex_t lock;
int readpos,writepos;
pthread_cond_t notempty;
pthread_cond_t notfull;
};
void init(struct prodcons *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->notempty,NULL);
pthread_cond_init(&b->notfull,NULL);
b->readpos = 0;
b->writepos = 0;
}
void put(struct prodcons *b,int data)
{
pthread_mutex_lock(&b->lock);
if((b->writepos + 1) % BUFFER_SIZE == b->readpos)
{
pthread_cond_wait(&b->notfull,&b->lock);
}
b->buffer[b->writepos] = data;
b->writepos++;
if(b->writepos >= BUFFER_SIZE)
{
b->writepos = 0;
}
pthread_cond_signal(&b->notempty);
pthread_mutex_unlock(&b->lock);
}
int get(struct prodcons *b)
{
int data;
pthread_mutex_lock(&b->lock);
if(b->writepos == b->readpos)
{
pthread_cond_wait(&b->notempty,&b->lock);
}
data = b->buffer[b->readpos];
b->readpos++;
if(b->readpos >= BUFFER_SIZE)
{
b->readpos = 0;
}
pthread_cond_signal(&b->notfull);
pthread_mutex_unlock(&b->lock);
return data;
}
#define OVER -1
struct prodcons buffer;
void * producer(void *data)
{
int n;
for(n = 0;n < 20;n++)
{
printf("%d----->\n",n);
put(&buffer,n);
}
put(&buffer,OVER);
return NULL;
}
void * consumer(void *data)
{
int d;
while(1)
{
d = get(&buffer);
if(d == OVER)
break;
printf("----->%d\n",d);
}
return NULL;
}
int main()
{
pthread_t th_a,th_b;
void *retval;
init(&buffer);
pthread_create(&th_a,NULL,producer,0);
pthread_create(&th_b,NULL,consumer,0);
pthread_join(th_a,&retval);
pthread_join(th_b,&retval);
return 0;
}
copyright (C), 2014-2015, Lighting Studio. Co., Ltd.
File name:
Author:Jerey_Jobs Version:0.1 Date:
Description:
Funcion List:
*****************************************************/
#include <stdio.h>
#include <pthread.h>
#define BUFFER_SIZE 16
struct prodcons
{
int buffer[BUFFER_SIZE];
pthread_mutex_t lock;
int readpos,writepos;
pthread_cond_t notempty;
pthread_cond_t notfull;
};
void init(struct prodcons *b)
{
pthread_mutex_init(&b->lock,NULL);
pthread_cond_init(&b->notempty,NULL);
pthread_cond_init(&b->notfull,NULL);
b->readpos = 0;
b->writepos = 0;
}
void put(struct prodcons *b,int data)
{
pthread_mutex_lock(&b->lock);
if((b->writepos + 1) % BUFFER_SIZE == b->readpos)
{
pthread_cond_wait(&b->notfull,&b->lock);
}
b->buffer[b->writepos] = data;
b->writepos++;
if(b->writepos >= BUFFER_SIZE)
{
b->writepos = 0;
}
pthread_cond_signal(&b->notempty);
pthread_mutex_unlock(&b->lock);
}
int get(struct prodcons *b)
{
int data;
pthread_mutex_lock(&b->lock);
if(b->writepos == b->readpos)
{
pthread_cond_wait(&b->notempty,&b->lock);
}
data = b->buffer[b->readpos];
b->readpos++;
if(b->readpos >= BUFFER_SIZE)
{
b->readpos = 0;
}
pthread_cond_signal(&b->notfull);
pthread_mutex_unlock(&b->lock);
return data;
}
#define OVER -1
struct prodcons buffer;
void * producer(void *data)
{
int n;
for(n = 0;n < 20;n++)
{
printf("%d----->\n",n);
put(&buffer,n);
}
put(&buffer,OVER);
return NULL;
}
void * consumer(void *data)
{
int d;
while(1)
{
d = get(&buffer);
if(d == OVER)
break;
printf("----->%d\n",d);
}
return NULL;
}
int main()
{
pthread_t th_a,th_b;
void *retval;
init(&buffer);
pthread_create(&th_a,NULL,producer,0);
pthread_create(&th_b,NULL,consumer,0);
pthread_join(th_a,&retval);
pthread_join(th_b,&retval);
return 0;
}
0 0
- 生产
- 生产类型
- 生产日报表
- 生产价格指数
- 生产产品
- 二维码生产
- 生产guid
- 生产消费
- 产品生产
- 生产消费
- 生产武器
- 流程生产与离散生产比较
- Mycat生产实践---生产环境部署
- 生产返工是什么?如何管理生产返工?
- 前端生产二维码(HTML生产二维码)
- 附加码生产程序
- 验证码生产程序
- 眼镜生产消费
- centos 7 安装Google earth
- Intersection of Two Linked Lists
- [NOIP2016] 愤怒的小鸟
- JAVA中堆和栈的区别
- 欢迎使用CSDN-markdown编辑器
- 生产
- 企业虚拟化的目的及本质
- Java中时间的比较总结
- js结合拍php建立倒计时功能(一个页面一个或者多个)
- HDU 2177 —— 取(2堆)石子游戏
- 文件校验
- strcpy的实现
- 近似计算
- (ZT)AndroidJNI 通过C++调用JAVA