#ifndef __PROCESS_THREAD_HPP__ #define __PROCESS_THREAD_HPP__ #include #include // For perror. #include // For abort. template struct ThreadLocal { ThreadLocal() { if (pthread_key_create(&key, NULL) != 0) { perror("Failed to create thread local, pthread_key_create"); abort(); } } ThreadLocal& operator = (T* t) { if (pthread_setspecific(key, t) != 0) { perror("Failed to set thread local, pthread_setspecific"); abort(); } return *this; } operator T* () const { return reinterpret_cast(pthread_getspecific(key)); } T* operator -> () const { return reinterpret_cast(pthread_getspecific(key)); } private: // Not expecting any other operators to be used (and the rest?). bool operator * (const ThreadLocal&) const; bool operator == (const ThreadLocal&) const; bool operator != (const ThreadLocal&) const; bool operator < (const ThreadLocal&) const; bool operator > (const ThreadLocal&) const; pthread_key_t key; }; #endif // __PROCESS_THREAD_HPP__