TLS thread local storing

来源:互联网 发布:税务数据共享 编辑:程序博客网 时间:2024/05/16 10:10
template<typename T>                                                                               class ThreadLocal                                                                                  {                                                                                                  public:                                                                                                ThreadLocal()                                                                                      {                                                                                                      pthread_key_create(&_pkey, &ThreadLocal::destructor);                                              (void)sizeof(T);                                                                               }                                                                                                  ~ThreadLocal()                                                                                     {                                                                                                      pthread_key_delete(_pkey);                                                                     }                                                                                                  T& value()                                                                                         {                                                                                                      T* thread_value = static_cast<T*>(pthread_getspecific(_pkey));                                     if (thread_value == NULL) {                                                                            T* new_obj = new T();                                                                              pthread_setspecific(_pkey, new_obj);                                                               thread_value = new_obj;                                                                           }                                                                                                     return *thread_value;                                                                             }                                                                                                     template <typename A1>                                                                                T& value(A1 arg1) {                                                                                       T* thread_value = static_cast<T*>(pthread_getspecific(_pkey));                                        if (!thread_value) {                                                                                      T* new_obj = new T(arg1);                                                                             pthread_setspecific(_pkey, new_obj);                                                                  thread_value = new_obj;                                                                           }                    return *thread_value;                                                                          }                                                                                                  ThreadLocal& operator=(const T& v)                                                                 {                                                                                                      value() = v;                                                                                       return *this;                                                                                  }                                                                                                  operator T() const                                                                                 {                                                                                                      T* thread_value = static_cast<T*>(pthread_getspecific(_pkey));                                     return *thread_value;                                                                          }                                                                                              private:                                                                                               static void destructor(void *x)                                                                    {                                                                                                      T* obj = static_cast<T*>(x);                                                                       delete obj;                                                                                    }                                                                                              private:                                                                                               pthread_key_t _pkey;                                                                           };                
0 0
原创粉丝点击