Reputation: 12452
while compiling, i got this error
expected 'union pthread_mutex_t *' but argument is type of 'pthread_mutex_t'
1) what is the difference between 'union pthread_mutex_t *' and 'pthread_mutex_t'?
2) How do i make 'pthread_mutex_t' into the correct argument?
void buffer_insert(int number)
{
pthread_mutex_t padlock;
pthread_cond_t non_full;
pthread_mutex_init(&padlock, NULL);
pthread_cond_init(&non_full, NULL);
if(available_buffer()){
put_in_buffer(number);
} else {
pthread_mutex_lock(padlock);
pthread_cond_wait(non_full, padlock);
put_in_buffer(number);
pthread_mutex_unlock(padlock);
pthread_cond_signal(non_empty);
}
}
Upvotes: 3
Views: 3862
Reputation: 500327
The asterisk in
int pthread_mutex_lock(pthread_mutex_t *mutex);
means that the function takes a pointer to pthread_mutex_t
.
You need to take the address of your mutex variable, i.e. replace padlock
with &padlock
when calling the function.
For example,
pthread_mutex_lock(padlock);
should read
pthread_mutex_lock(&padlock);
and so on (for both the mutex and the condition variable).
It is also worth noting that in the code you show, padlock
and non_full
are local to the function, and are created and destroyed every time the function is called. Therefore no synchronization is taking place. You need to rethink how you declare and initialize the two variables.
There are further problems with the code. For example, the way you're using the condition variable is flawed in several respects.
With this in mind, I'd recommend following a pthreads tutorial.
Upvotes: 6