Reputation: 24768
https://computing.llnl.gov/tutorials/pthreads/samples/join.c
I am looking at the pthreads code at the link above.
I am not able to understand the type casting that is being done in the code above for variable t and status in BusyWork and main methods.
From man pages for pthread_exit:
The value pointed to by retval should not be located on the calling thread's stack, since the contents of that stack are undefined after the thread terminates.
So I guess the type casting in the example is trying to avoid referring memory on the stack of thread that has just terminated. But I find that typecasting quite inconvenient and bizzare. Can someone explain?
Upvotes: 0
Views: 245
Reputation: 1139
When you create a thread with pthread_create, your argument is limited to sizeof(void *)
, you usually use it to pass a pointer to a struct or any other data that may be bigger than the size of argument.
In this example, the argument long t
have a size smaller than sizeof(void *)
, so you can simply pass your argument directly to the pthread_create() call.
The explicit cast is made to ensure that if there is any difference between the memory allocated for type (long) or type (void *), it will be fixed by the compiler.
In practice, those cast will do nothing in most platform, but you cannot guarantee that, so you must explicit cast your argument to the correct type before using it.
Resuming: the example is using the type void *
as a type long
, and not as a literal pointer, so the variable status
do not dereference anything.
Upvotes: 1