Do you find it strange that pthread_setspecific
needs const void pointer but pthread_getspecific
returns non-const void pointer?
void *pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);
Do you find it strange that pthread_setspecific
needs const void pointer but pthread_getspecific
returns non-const void pointer?
void *pthread_getspecific(pthread_key_t key);
int pthread_setspecific(pthread_key_t key, const void *value);
Meh, just another const-unsafe C interface. In truth, though, it's quite impossible to create a const
-safe version of this. If both functions are consistent, then either people who want to store pointers-to-const
must cast away const
when storing the pointer, or people who want to store pointers-to-non-const
must cast away const
when retrieving the pointer.
The way it is, it's not safe, but at least it doesn't force you to clutter the code with casts.
pthread_setspecific
a const
pointer, and you can get it back non-const
via pthread_getspecific
, so if your original pointer was const
now you get the const
casted away.
Oct 11, 2011 at 13:03
pthread_getspecific()
points to the same object as does the pointer given to pthread_setspecific()
, then this makes sense.
Oct 11, 2011 at 13:11
It is to indicate that the value pointed to is not modified by pthread_setspecific(). You are free to modify the value pointed to by the return of pthread_getspecific().
const
unsafe (pthread_getspecific
drops the const
that might have been on the original pointer); still, any other way would be a useless complication.
Oct 11, 2011 at 13:09
pthread_setspecific
only states, that this function doesn't modify value
. You can pass it non-const stuff..
const
pointers, but pthread_getspecific
returns a non-const
pointer in any case, thus discarding the const
if it was present.
Oct 11, 2011 at 13:26
These are getter and setter functions whose prototypes remain like this across many languages.
pointer to constant something
, not aconstant pointer to something
.