c - 生产者和消费者在数字生成器中没有正确关闭

标签 c random pthreads

所以我正在为使用基本 GNU 随机数生成器的作业制作一个数字生成器。理想情况下,我需要输出处于这个开发阶段

On loop 0, just produced...1804289383
On loop 0, I just consumed...1804289383.

等等,但现在,我的输出是:

On loop 0, just produced...1804289383
On loop 1, just produced...846930886
On loop 2, just produced...1681692777
On loop 3, just produced...1714636915
On loop 4, just produced...1957747793
On loop 5, just produced...424238335
On loop 6, just produced...719885386
On loop 7, just produced...1649760492
On loop 8, just produced...596516649
On loop 9, just produced...1189641421
On loop 0, I just consumed...1804289383
On loop 1, I just consumed...846930886
On loop 2, I just consumed...1681692777
On loop 3, I just consumed...1714636915
On loop 4, I just consumed...1957747793
On loop 5, I just consumed...424238335
On loop 6, I just consumed...719885386
On loop 7, I just consumed...1649760492
On loop 8, I just consumed...596516649
On loop 9, I just consumed...1189641421

直到产生一堆数字后,它才会打动消费者。

这是怎么回事?我一直在将它与另一段执行类似功能的代码进行比较,那段代码完成了我需要做的事情,这两段代码非常相似。我只需要另一双眼睛,也许我错过了一些简单的东西?

我的代码:

#include <pthread.h>
#include <time.h>
#include <stdio.h>
#include <math.h>
#define MAX_LOOP 10 //how many times the loop is allowed to go
#define MAX_VALUE 10 //how hig the random number can be
#define TRUE 1 //boolean value
#define FALSE 0 // ""
#define BUFFER_SIZE 10
#define DEBUG TRUE //debug variable. if on 1, it checks for bugs and stuff
pthread_cond_t cond; // conditional variable global
pthread_mutex_t the_mutex;

int bufferRandomFirst[BUFFER_SIZE]; //shared buffer array

int numOfItemsInQueue = 0;//number of items in the array



void *producerRandom(void *ptr)
{
/*
*
*   Random() Producer function:
*   Creates numbers for the consumer to process using gcc random()
*
*/
    int i = 0;
    int rand_num = 0;
    int rear = 0; //beginning of queue

    srandom((unsigned int)0) ; //make some random seeds

    for (i = 0; i< BUFFER_SIZE; i++){   

        rand_num =random(); //make a random number

        pthread_mutex_lock(&the_mutex); //get access to the buffer
        while (numOfItemsInQueue ==BUFFER_SIZE) {
            pthread_cond_wait(&cond, &the_mutex); //if the buffer is not empty, producer must stop
        }//end of while

        bufferRandomFirst[rear] = rand_num; //store the number in the buffer
        if (DEBUG){
            printf("On loop %d, just produced...%d\n", i, bufferRandomFirst[rear]);
        }
        rear = (rear + 1) % BUFFER_SIZE; //does the ciruclar queue loop
        numOfItemsInQueue +=1;

        pthread_cond_signal(&cond); //fires up the consumer process
        pthread_mutex_unlock(&the_mutex); // unlocks the mutex

    }//end of for loop                                                                      
    pthread_exit(0); //exit the thread
}//end producer


void *consumerRandom(void *ptr)
{
/*
*
*   Random() Consumer Function
*   consumes the numbers that the producer makes
*
*/
    int front = 0; //front of the queue
    int lastDigit = 0; //last digit of random number (thanks Nathan)
    int i = 0; //counting variable for for loops

    for (i = 0; i < BUFFER_SIZE; i++){

        pthread_mutex_lock(&the_mutex); //get exclusive access to buffer

        while(numOfItemsInQueue == 0){
            pthread_cond_wait(&cond, &the_mutex);
        }//end of while
        if (DEBUG){
            printf("On loop %d, I just consumed...%d\n", i, bufferRandomFirst[i]);
                }

        lastDigit = bufferRandomFirst[front] % 10; //steals the last digit from the random number
                    //for example, if the number was 100, 100 mod 10
                    //is 0, so you get 0.  if it was 101, you would get 1


        front = (front + 1) % BUFFER_SIZE; //wrap around the array
        numOfItemsInQueue -= 1;
        pthread_cond_signal(&cond);//wake up producer
        pthread_mutex_unlock(&the_mutex);

    }//end of for loop
    pthread_exit(0); //exit the loop
}//end of consumer




int main(int argc){
    pthread_t thread_one, thread_two; //create two threads for Random() Function


    printf("Random GNU numbers inbound!\n");
    //Random GCC pthread handing/
    pthread_mutex_init(&the_mutex,0); //set up the mutex    
    pthread_cond_init(&cond, 0); //set up the condition
    //makes a thread
    pthread_create(&thread_one, 0, producerRandom, 0);
    //makes a thread
    pthread_create(&thread_two, 0, consumerRandom, 0);  
    pthread_join(thread_one, 0); //wait on this thread  
    pthread_join(thread_two, 0); //wait on this one too. =)
    pthread_cond_destroy(&cond); //condition destroy
    pthread_mutex_destroy(&the_mutex); //destroy the mutex resources
    printf("And that is all for those numbers\n\n");

}//end of main

最佳答案

从我最初浏览您的源代码开始,生产者和消费者之间不存在会导致您期望的行为的互锁。例如,在生产者中,没有任何内容告诉它要等到一个项目被消耗后再生产另一个项目。 the_mutex 在生产者循环结束时释放,但在循环顶部立即重新获取。如果 OS 调度程序决定您的生产者线程仍然可以继续运行,那么将生产另一个项目。

关于c - 生产者和消费者在数字生成器中没有正确关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19781868/

相关文章:

java - GCC 和 JVM 自动并行批处理

java - 根据返回值计算平均值

javascript - 用于表单的 Google Apps 脚本,生成唯一 ID 号

c - 程序收到信号 SIGPIPE,Broken pipe

c - 在 C 中生成稀疏矩阵

c++ - 默认情况下整数文字的类型不是 int?

c - 给函数内部的指针赋值

java - 基于哈希创建一个统一的随机数

c - 传递二维数组进行线程处理

c - 在 C 中使用互斥锁同步两个 pthread