#ifndef BOO_THREADLOCALPTR_HPP #define BOO_THREADLOCALPTR_HPP #if _WIN32 #else #include #endif /** Multiplatform TLS-pointer wrapper (for compilers without proper thread_local support) */ template class ThreadLocalPtr { #if _WIN32 DWORD m_key; public: ThreadLocalPtr() {m_key = TlsAlloc();} ~ThreadLocalPtr() {TlsFree(m_key);} T* get() const {return static_cast(TlsGetValue(m_key));} void reset(T* v=nullptr) {TlsSetValue(m_key, LPVOID(v));} #else pthread_key_t m_key; public: ThreadLocalPtr() {pthread_key_create(&m_key, nullptr);} ~ThreadLocalPtr() {pthread_key_delete(m_key);} T* get() const {return static_cast(pthread_getspecific(m_key));} void reset(T* v=nullptr) {pthread_setspecific(m_key, v);} #endif T* operator->() {return get();} }; #endif // BOO_THREADLOCALPTR_HPP