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


.h file :

class redisthreadlocalclient {  public:      redisthreadlocalclient() {}     virtual ~redisthreadlocalclient();  public:     static void destroy_client(void* client) {         redissyncclient* c = static_cast<redissyncclient*>(client);         delete c;     }         static int init(const std::vector<std::string>& address) {         _s_address = address;         return pthread_key_create(&_s_thread_key, redisthreadlocalclient::destroy_client);       }        static redissyncclient* get_client();  private:      static pthread_key_t _s_thread_key;       static std::vector<std::string> _s_address;  };  

.cc file

pthread_key_t redisthreadlocalclient::_s_thread_key; std::vector< std::string> redisthreadlocalclient::_s_address;  redissyncclient* redisthreadlocalclient::get_client() {     redissyncclient* client = static_cast<redissyncclient*>(pthread_getspecific(_s_thread_key));     if (client != null) // here     {         return client;     }      redissyncclient* c = new redissyncclient();     if (c->init(_s_address) != 0)     {         delete c;         return null;     }      pthread_setspecific(_s_thread_key, c);     return c; } 

why if ( client != null ) evaluating true when first called in function? thought if haven't-yet called pthread_setspecific current thread, null returned pthread_getspecific , therefore result false ?

what missing?

pthread_key_create should called precisely once, before use of pthread_getspecific or pthread_setspecific key.

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

the pthread_key_create() call either explicitly made in module initialization routine, or can done implicitly first call module [using pthread_once].


Comments

Popular posts from this blog

How to provide Authorization & Authentication using Asp.net, C#? -

toolbar - How to add link to user registration inside toobar in admin joomla 3 custom component -

How to use Authorization & Authentication in Asp.net, C#? -