多线程消费者,生产者C代码,不执行?

时间:2012-01-04 02:48:58

标签: c multithreading mutex consumer producer

我正在尝试做一个简单的多线程消费者/生产者,其中多个读取器和写入器线程,从文件读取到缓冲区,然后从缓冲区读回文件。它应该是线程安全的。然而,它没有像我预期的那样表现。它停止了一半,但每次都停在不同的线路上? 请帮我理解我做错了什么?!?

#include <pthread.h>
#include <stdio.h>
#include <stdlib.h>
//TODO Define global data structures to be used
#define BUF_SIZE 5
FILE *fr;
FILE *to;            /* declare the file pointer */

struct _data {
    pthread_mutex_t mutex;
    pthread_cond_t cond_read;
    pthread_cond_t cond_write;
    int condition;
    char buffer[BUF_SIZE];
    int datainbuffer;
}dc1 = {
    PTHREAD_MUTEX_INITIALIZER,PTHREAD_COND_INITIALIZER,PTHREAD_COND_INITIALIZER,0,{0},0
};


void *reader_thread(void *arg) {
    //TODO: Define set-up required
    struct _data *d = (struct _data *)arg;
    int killreaders = 0;
    while(1) {
        //TODO: Define data extraction (queue) and processing

        pthread_mutex_lock(&d->mutex);

        while (d->condition == 0 || d->datainbuffer<=0){
            pthread_cond_wait( &d->cond_read, &d->mutex );
            if(killreaders == 1){
                pthread_mutex_unlock(&d->mutex);
                pthread_cond_signal(&d->cond_read);
                pthread_cond_signal(&d->cond_write);
                return NULL;
            }

        }
        d->condition = 0;

        int i;
        char res;
        //if the buffer is not full, that means the end of file is reached and it time to kill the threads remaining.
        if(d->datainbuffer!=BUF_SIZE)
            killreaders = 1;

        for (i=0; i<(sizeof d->datainbuffer); i++) {
            res = d->buffer[i];
            printf("to file:%c",res);
            fputc(res, to);
        }
        d->datainbuffer = 0;


        pthread_mutex_unlock(&d->mutex);
        pthread_cond_signal( &d->cond_write );


    }

    return NULL;
}

void *writer_thread(void *arg) {
    //TODO: Define set-up required
    struct _data *d = (struct _data *)arg;
    char * pChar;
    int killwriters = 0;

    while(1){
        pthread_mutex_lock(&d->mutex);
        while( d->condition == 1 || d->datainbuffer>0){
            pthread_cond_wait( &d->cond_write, &d->mutex );
            if(killwriters==1){
                pthread_mutex_unlock(&d->mutex);
                pthread_cond_signal(&d->cond_write);
                pthread_cond_signal(&d->cond_read);
                return NULL;

            }
        }
        d->condition = 1;
        int i;
        char rc;
        for (i = 0; i < BUF_SIZE; i++){
            if((rc = getc(fr)) == EOF){
                killwriters = 1;
                pthread_mutex_unlock(&d->mutex);
                pthread_cond_signal(&d->cond_read);

                return NULL;
            }
            d->datainbuffer = i+1;
            d->buffer[i] = rc;
            printf("%c",rc);
        }

        int m = 0;

        pthread_mutex_unlock(&d->mutex);
        pthread_cond_signal(&d->cond_read);


    }


    return NULL;
}


#define M 10
#define N 20
int main(int argc, char **argv) {
    struct _data dc=dc1;

    fr = fopen ("from.txt", "rt");  /* open the file for reading */
    if (fr == NULL)
    {
        printf("Could not open file!");
        return 1;
    }
    to = fopen("to.txt", "wt");


    int i;
    pthread_t readers[N];
    pthread_t writers[M];


    for(i = 0; i < N; i++) { 
        pthread_create(&readers[i], NULL, reader_thread, (void*)&dc);
    }

    for(i = 0; i < M; i++) { 
        pthread_create(&writers[i], NULL, writer_thread, (void*)&dc);
    }
    fclose(fr);
    fclose(to);

    return 0;   
}

任何建议都表示赞赏!

3 个答案:

答案 0 :(得分:3)

您的主题正在读取和写入您打开的文件。在主要关闭。但是在关闭这些文件之前,main没有明确等待线程完成。

答案 1 :(得分:3)

除了Scott Hunter指出的问题之外,你的读者和作者在持有互斥体的同时完成了所有“实际工作”,首先打败了拥有多个线程的点。

读者应如下操作:

1)获取互斥锁。
2)阻止条件变量直到工作可用 3)从队列中删除工作,可能是信号条件变量 4)释放互斥锁。
5)处理工作 6)转到步骤1.

作家应按如下方式运作:

1)获取我们需要写的信息 2)获取互斥锁。
3)阻塞条件变量,直到队列中有空间为止 4)将信息放入队列中,可能是信号条件变量 5)释放互斥锁。
6)转到步骤1.

请注意两个线程在没有持有互斥锁的情况下执行“实际工作”?否则,为什么有多个线程,如果其中只有一个可以一次工作?

答案 2 :(得分:0)

我不确定我的答案是否会对你有所帮助..但我会尽量给你一些参考代码。

我编写了一个类似的程序(除了它不写入文件,而是显示stdout中的queue- / produce- / consume-项目)。它可以在这里找到 - https://github.com/sangeeths/pc。我已将命令行处理和队列逻辑分离为单独的文件。

希望这有帮助!