返回顶部

收藏

生产者和消费者

更多
#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