# 生产者和消费者

```#include <pthread.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#define MAX_LEN 10

#define OFF 0x00000000
#define ON  0x00000001

#define TRUE 0x00000000
#define FALSE 0x00000001

#define EMPTY 0xFFFFFFFF
#define FULL -2

#define USECOND 1
#define MSECOND 1000 * USECOND
#define SECOND 1000 * MSECOND

#define PRODUCER_INTERVAL 1  * MSECOND // unit is MS
#define CONSUMER_INTERVAL 500 * MSECOND // unit is MS

int lock = OFF;
int queue[MAX_LEN] = {0};
int number = 0;
int max_number = 10000;
int is_prime(int input)
{
int i = 0, temp = 0;

if (input <= 1) { // invalid input
return FALSE;
}
else if (input == 2) { // 2 is a prime
//fprintf(stderr, "input [%d]\\n", input);
return TRUE;
}
else { // valid input
temp = (int)sqrt(input);
for (i = 2; i <= temp; i++) {
if (input % i == 0) { // if true, then input not a prime
return FALSE;
}
}
//fprintf(stderr, "input [%d]\\n", input);
return TRUE;
}
}
int is_locked(void *input)
{
if (lock == ON)
return TRUE;
return FALSE;
}
int lock_lock(void *input)
{
lock = ON;
return 0;
}
int unlock_lock(void *input)
{
lock = OFF;
return 0;
}
int produce_item(void *input)
{
int pro_i = 0;
if (FALSE == is_prime(number++)) {
return FALSE;
}
for (pro_i = 0; pro_i < MAX_LEN; pro_i++) {
if (queue[pro_i] == EMPTY) {
queue[pro_i] = number - 1;
return TRUE;
}
}
return FALSE;
}
int consume_item(void *input)
{
int con_i = 0;
for (con_i = 0; con_i < MAX_LEN; con_i++) {
if (queue[con_i] != EMPTY) {
fprintf(stderr, "<<<consumer: eat a food [%d]\\n", queue[con_i]);
queue[con_i] = EMPTY;
return TRUE;
}
}
return FALSE;
}

void* producer(void *input)
{
int pro_ret = 0;
fprintf(stderr, ">>>producer: producer start\\n");
while(1) {
usleep(PRODUCER_INTERVAL);
if (is_locked(NULL) == TRUE) { // if lock then continue wait for
fprintf(stderr, "producer: locked\\n");
continue;
}
else {
lock_lock(NULL);
pro_ret = produce_item(NULL);
if (TRUE == pro_ret) { // if produce success then unlock lock
fprintf(stderr, ">>>producer: generate a food [%d]\\n", number - 1);
}
else if (FALSE == pro_ret) {
fprintf(stderr, ">>>producer: ****generate failed [%d]****\\n", number - 1);
}
else if (FULL == pro_ret){
fprintf(stderr, ">>>producer: ****queue is full****\\n");
}
unlock_lock(NULL);
}
}
fprintf(stderr, "producer: producer end\\n");
return NULL;
}
void* consumer(void *input)
{
fprintf(stderr, "<<<consumer: consumer start\\n");
while(1) {
usleep(CONSUMER_INTERVAL);
if (is_locked(NULL) == TRUE) { // if lock then continue wait for
fprintf(stderr, "<<<consumer: locked\\n");
continue;
}
else {
lock_lock(NULL);
if (TRUE == consume_item(NULL)) { // if produce success then unlock lock
}
else {
fprintf(stderr, "<<<consumer: ****queue is empty****\\n");
}
unlock_lock(NULL);
}
}
fprintf(stderr, "<<<consumer: consumer end\\n");
return NULL;
}
int main(int argc, char *argv[])
{
//is_prime(7);
memset(queue, EMPTY, sizeof(queue));
fprintf(stderr, "all start!\\n");

sleep(0xFFFFFFFF);

return 0;
}
//该片段来自于http://outofmemory.cn
```

0人收藏

0

0

1. tanglei 发表 2014-05-28 15:08:01 struct与class区别联系
2. 博主 发表 2016-06-28 05:17:59 Bazel C++ 基础[翻译]
3. 博主 发表 2011-11-03 16:00:00 C++ 语言基础
4. thinkpc 发表 2016-03-04 13:55:30 c++&nbsp;11 map基础value排序
5. pansunyou 发表 2014-11-30 02:51:00 C++通用跨数据库访问方案之一: 基础组件cdbc
6. linux@linux.cn (linu 发表 2016-11-04 02:37:00 C++ 程序员 Protocol Buffers 基础指南
7. Qiang 发表 2011-09-08 14:06:35 Lexing Python Indentation using Spirit.Lex
8. One Coder 发表 2013-01-26 14:53:18 C++ 语法特性学习小记
9. mortoray 发表 2013-03-25 04:27:22 Why do we need pointers/references?
10. Herb Sutter 发表 2012-06-05 19:10:01 GotW #105: Smart Pointers, Part 3 (Difficulty: 7/1
11. Herb Sutter 发表 2012-11-06 21:11:12 Friday’s Q&A session now online
12. Boris Kolpackov 发表 2012-11-22 12:15:22 ODB 2.1.1 Released