I've been looking at Advanced Linux Programming by Mitchell, Oldham and Samuel. I've seen in the section on pthreads something about void pointers and casting that confuses me.
Passing an argument to pthread_create(), they don't cast the pointer to a void pointer even though that is what the function expects.
pthread_create( &thread, NULL, &compute_prime, &which_prime );
Here, which_prime is of type int.
But taking a value returned from the thread using pthread_join, they DO cast the variable to void pointer.
pthread_join( thread, (void*) &prime );
Here, prime is of type int again.
Why is casting done in the second instance and not in the first?
pthread_join
's second argument is a void**. That code looks wrong. - John Zwinckvoid*
returned by the thread function and received bypthread_join()
asint
. This is the relevant line ofcompute_prime()
:int candidate; ... return (void*) candidate;
So using&prime
as 2nd argument toint prime; ... pthread_join(..., &prime)
perfectly makes sense. However casr it tovoid*
simply is wrong. If placeing a castvoid**
would have been valid, as by the declaration ofpthread_join(pthread_t, void **)
. - alk