I am learning how threading and locking works. To do so, I am writing a producer-consumer threading program in C that utilizes mutex locks. The object of the program is to receive numbers and produce a multiplication product.
1- The program takes in numbers, from a file, or command line.
2- The main thread passes the numbers to the child thread, 3 numbers at a time.
3- Outputs the result
I am getting a bad access error in my child thread as I am passing in the three numbers and I am not sure why. I don't think that I am using the mutex lock and unlock correctly. Please help.
#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;
}
There are multiple issues in your program, Let me list all of them.
child_thread
instead of a address data
. child_thread
you don't need to lock access to data structure since it is not shared. data->numGroups
since result array contains that many elements. Following snippet shows the working version of your code, You can comment out locking and unlocking methods inside the while loop.
#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;
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.