返回顶部

收藏

生产者和消费者

更多
#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[])
{
    pthread_t task_producer, task_consumer;
    //is_prime(7);
    memset(queue, EMPTY, sizeof(queue));
    pthread_create(&task_producer, NULL, producer, NULL);
    pthread_create(&task_consumer, NULL, consumer, NULL);
    fprintf(stderr, "all start!\\n");

    sleep(0xFFFFFFFF);

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

标签:c++,基础

收藏

0人收藏

支持

0

反对

0

»更多 您可能感兴趣的代码
  1. 2012-11-05 21:59:42java获得随机数代码 by 怪兽狂殴奥特曼
  2. 2014-05-09 15:38:47内核定时器 by 灵剑子
  3. 2014-05-10 17:17:07万年历查询系统C++ by 灵剑子
  4. 2014-05-11 19:21:15list 函数 by 跳跳虎
  5. 2014-05-14 15:02:52C++解决易语言难题 by 跳跳虎
  6. 2014-05-16 19:41:15一个找出指定分隔符左侧的子字符串的函数 by 小项
  7. 2014-05-17 18:01:40ACM poj 2447 RSA by 跳跳虎
  8. 2014-05-18 15:04:45linux下串口编程 by walker30
  9. 2014-05-20 11:19:52简单程序包括各种邻接矩阵操作 by niutao.linux
  10. 2014-05-20 15:26:23Reading UTF-8 with C++ streams by 千万不要郁闷
  11. 2014-05-21 18:41:15指针运算 by 童学芬
相关聚客文章
  1. 博主 发表 2011-11-03 16:00:00 C++ 语言基础
  2. thinkpc 发表 2016-03-04 13:55:30 c++&nbsp;11 map基础value排序
  3. pansunyou 发表 2014-11-30 02:51:00 C++通用跨数据库访问方案之一: 基础组件cdbc
  4. linux@linux.cn (linu 发表 2016-11-04 02:37:00 C++ 程序员 Protocol Buffers 基础指南
  5. tanglei 发表 2014-05-28 15:08:01 struct与class区别联系
  6. 博主 发表 2016-06-28 05:17:59 Bazel C++ 基础[翻译]
  7. Herb Sutter 发表 2013-03-14 15:50:07 Words of wisdom: Bjarne Stroustrup
  8. SumitMSFT 发表 2013-03-22 22:12:00 Visual C++ Developer Survey
  9. bystander 发表 2013-04-11 10:50:25 模板栈以及中缀表达式求值(C++实现)
  10. mortoray 发表 2013-05-09 10:22:13 How to catch a “return” statement
  11. Qiang 发表 2011-09-08 14:06:00 Lexing Python Indentation using Spirit.Lex
  12. 博主 发表 2013-06-15 20:33:32 CloudStack云基础架构的一些概念

发表评论