0
votes

I cannot figure out how correctly pass my queue pointer to a pthread_create function.

Here is snippet of my code:

queue_t *queue_arr[number_of_categories];
int i = 0;
for(i = 0; i < number_of_categories; i++){
    queue_arr[i] = queue_create();
}

trying to create thread with a pointer to a queue in a loop: //loop and some code here

if (pthread_create(&tid[i+1], NULL, consumer_thread, (void *)queue_arr[i]) != 0 ){
    perror("pthread_create for consumer failed");
    exit(-1);
};

it gives me a warning:

Incompatible pointer types passing 'void *(queue_t)' to parameter of type 'void *(*)(void *)'

Not sure how would I change queue_arr[i] in a correct manner for pthread_create.

Thanks for help guys

1
Read the warning message again, it's not the (void *)queue_arr[i] expression that the warning is about. Hint: Check the consumer_thread function prototype/definition and see if you can find anything wrong with it. - Some programmer dude

1 Answers

2
votes

Your function prototype/definition for consumer_thread should be like void* consumer_thread(void* arg) and pthread_create statement as pthread_create(&tid[i+1], NULL, consumer_thread, (void *)queue_arr[i]). Finally in your consumer_thread function you can typecast your arg back to queue_t* datatype.