多线程使用者、生产者C代码,不执行



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

#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;   
}

欢迎提出任何建议!

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

除了Scott Hunter指出的问题外,您的读者和作者在持有互斥体的同时完成了所有的"实际工作",从而克服了最初拥有多个线程的问题。

读卡器应按以下方式操作:

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

写入程序应按以下方式操作:

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

注意到两个线程都在不持有互斥的情况下完成"真正的工作"吗?否则,如果一次只有一个线程可以工作,为什么要有多个线程?

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

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

希望这能有所帮助!

最新更新