Эта функция выполняется потоком.Я полагаю, что ошибка возникает, когда я пытаюсь получить доступ к rand_count, который является переменной структуры, называемой args.Обратите внимание, что проблема находится в цикле for производителя.
void* Producers(void* fargs) {
printf("I am Producer and I am not locked\n");
args* f_args=(args*) fargs;
rc = pthread_mutex_lock(&Mutex);
printf("I am Producer and I am locked\n");
if (rc != 0) {
printf("ERROR: return code from pthread_mutex_lock() is %d\n", rc);
pthread_exit(&rc);
}
printf(" The ruler of all mutexes has passed from here and shall start creating random numbers\n");
for(int i=0; i < *f_args->rand_count; ++i) {
printf ( "Creating random number\n");
my_random =rand_r(f_args->seed)%255;
if(i==cb->capacity) {
printf(" Your producer king will now sleep\n");
rc = pthread_cond_signal(&Condition);
if (rc != 0) {
printf("ERROR: return code from pthread_cond_signal() is %d\n", rc);
pthread_exit(&rc);
}
rc = pthread_cond_wait(&Condition, &Mutex);
if (rc != 0) {
printf("ERROR: return code from pthread_cond_wait() is %d\n", rc);
pthread_exit(&rc);
}
}
cb_push_back(cb, &my_random);
if(i==0) {
fp=fopen("prod_in.txt", "w");
} else {
fp=fopen("prod_in.txt", "a");
}
fprintf(fp, "Producer %d: %d\n", f_args->ID, my_random);
}
rc = pthread_cond_signal(&Condition);
if (rc != 0) {
printf("ERROR: return code from pthread_cond_signal() is %d\n", rc);
pthread_exit(&rc);
}
rc = pthread_mutex_unlock(&Mutex);
if (rc != 0) {
printf("ERROR: return code from pthread_mutex_unlock() is %d\n", rc);
pthread_exit(&rc);
}
}
Это структура
typedef struct args{
int ID; //Unique ID from 1 to n
int* seed; //The seed used for rand_r
int* rand_count; //The number of random generated numbers
}args;