Я изучаю, как работают потоки и блокировки. Для этого я пишу программу потоков продюсер-потребитель на C, которая использует блокировки мьютекса. Цель программы - получить числа и произвести произведение умножения.
1- Программа принимает числа из файла или командной строки.
2 - основной поток передает числа дочернему потоку, по 3 числа за раз.
3 - выводит результат
Я получаю ошибку доступа в моем дочернем потоке, поскольку я передаю три числа, и я не уверен, почему. Я не думаю, что я использую блокировку mutex и разблокировать правильно. Пожалуйста, помогите.
#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;
}