Skip to content
Advertisement

pthread_getspecific(key) get not NULL result, but not call pthread_setspecific(key), why?

.h file :

JavaScript

.cc file

JavaScript

Why is if ( client != NULL ) evaluating as true when first called in this function? I thought if I haven’t-yet called pthread_setspecific for the current thread, NULL would be returned from pthread_getspecific and therefore the result would be false ?

What am I missing?

Advertisement

Answer

pthread_key_create should be called precisely once, before any use of pthread_getspecific or pthread_setspecific with that key.

From http://pubs.opengroup.org/onlinepubs/009695399/functions/pthread_key_create.html:

The pthread_key_create() call could either be explicitly made in a module initialization routine, or it can be done implicitly by the first call to a module [using pthread_once].

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement