如何使用互斥锁在C中执行生产者-消费者程序

时间:2018-11-02 18:40:26

标签: c pthreads mutex

我正在学习线程和锁定的工作方式。为此,我正在用C编写使用互斥锁的生产者-消费者线程程序。该程序的目的是接收数字并产生一个乘积。

1-程序从文件或命令行中输入数字。

2-主线程将数字传递给子线程,一次传递3个数字。

3-输出结果

在传递三个数字时,我的子线程访问错误,我不确定为什么。我认为我没有正确使用互斥锁和解锁。请帮忙。

#include<stdio.h>
#include<stdlib.h>
#include<pthread.h>

#define SIZE (20)

struct data_t {
    int numGroups;
    int data[SIZE];
    pthread_mutex_t mutex;
};

/* Simple child thread function */
void *child_thread(void *p) {
    struct data_t *data = (struct data_t *)p;

    int *result = malloc(33*sizeof(int));

    for (int i = 0; i < data->numGroups; ++i) {
        pthread_mutex_lock(&data->mutex);
        result[i] = data->data[i*3] * data->data[i*3+1] * data->data[i*3+2]; // <- I am getting a bad access error on this line
        pthread_mutex_unlock(&data->mutex);
    }

    return result;
}

int main(int argc, char *argv[]) {
    FILE *fp = stdin;
    struct data_t data;
    data.numGroups = 0;
    pthread_t thread_handle;
    void *result;

    // Validate the file
    if ( argc > 1 ) {
        fp = fopen(argv[1], "r");
    }
    if ( fp == NULL ) {
        printf("Couldn't open the file %s\n", argv[1]);
        perror("Trying to open file");
        return -1;
    }

    int num1, num2, num3;

    while (fscanf(fp, "%d %d %d", &num1, &num2, &num3) == 3) {
        pthread_mutex_lock(&data.mutex);
        data.data[data.numGroups*3] = num1;
        data.data[data.numGroups*3+1] = num2;
        data.data[data.numGroups*3+2] = num3;
        data.numGroups++;
        pthread_mutex_unlock(&data.mutex);
    }

    /* Create child thread */
    pthread_create(&thread_handle, NULL, child_thread, &data.mutex);

    /* Retrieve result by passing a reference to a void pointer */
    pthread_join(thread_handle, &result);

    int *output = (int *)result;

    for (int i = 0; i < data.numGroups; ++i) {
        printf("The product of %d, %d, %d is %d\n", data.data[i*3], data.data[i*3+1], data.data[i*3+2], output[i]);
    }

    /* Free allocated memory */
    free(result);

    return 0;
}

1 个答案:

答案 0 :(得分:0)

您的程序中有多个问题,让我列出所有问题。

  1. 程序的一个问题是您要将互斥量的地址传递给 child_thread而不是地址data
  2. 另一个问题是,由于您在创建之前要填充结构 child_thread您不需要锁定对数据结构的访问权限,因为 它没有共享。
  3. 在子线程中分配的结果内存大小等于 data->numGroups,因为结果数组包含那么多元素。

下面的代码片段显示了代码的工作版本,您可以在while循环中注释掉锁定和解锁方法。

    #include <stdio.h>
    #include <stdlib.h>
    #include <pthread.h>

    #define SIZE (20)

    struct data_t {
        int numGroups;
        int data[SIZE];
        pthread_mutex_t mutex;
    };

    /* Simple child thread function */
    void *child_thread(void *p)
    {
        struct data_t *data = (struct data_t *)p;
        int *result = malloc(data->numGroups*sizeof(int));
        for (int i = 0; i < data->numGroups; ++i) {
            pthread_mutex_lock(&data->mutex);
            result[i] = data->data[i*3] * data->data[i*3+1] * data->data[i*3+2];
            pthread_mutex_unlock(&data->mutex);
        }
        return result;
    }

    int main(int argc, char *argv[]) {
        FILE *fp;
        struct data_t data;
        data.numGroups = 0;
        pthread_t thread_handle;
        void *result;

        // Validate the file
        if ( argc > 1 ) {
            fp = fopen(argv[1], "r");
        }
        if ( fp == NULL ) {
            printf("Couldn't open the file %s\n", argv[1]);
            perror("Trying to open file");
            return -1;
        }

        int num1, num2, num3;

        while (fscanf(fp,"%d %d %d", &num1, &num2, &num3) == 3) {
            pthread_mutex_lock(&data.mutex); //Actually not necessary, since child thread is not running.
            data.data[data.numGroups*3] = num1;
            data.data[data.numGroups*3+1] = num2;
            data.data[data.numGroups*3+2] = num3;
            data.numGroups++;
            pthread_mutex_unlock(&data.mutex);//Actually not necessary, since child thread is not running.
        }

        /* Create child thread */
        pthread_create(&thread_handle, NULL, child_thread, (void*)&data);

        /* Retrieve result by passing a reference to a void pointer */
        pthread_join(thread_handle, &result);
        int *output = (int *)result;

        for (int i = 0; i < data.numGroups; ++i) {
            printf("The product of %d, %d, %d is %d\n", data.data[i*3], data.data[i*3+1], data.data[i*3+2], output[i]);
        }

        /* Free allocated memory */
        free(result);

        return 0;
    }