14 #ifndef THREADLOCAL_H_
15 #define THREADLOCAL_H_
19 #if defined __GNUC__ && __GNUC__ >= 4
24 #include <QThreadStorage>
34 template <
typename ...Arg>
35 XThreadLocal(Arg&& ...arg) : m_var(std::forward<Arg>(arg)...) {}
41 static T thread_local m_var;
55 mutable QThreadStorage<T*> m_tls;
58 mutable pthread_key_t m_key;
59 static void delete_tls(
void *var);
70 if( !m_tls.hasLocalData())
71 m_tls.setLocalData(
new T);
72 return *m_tls.localData();
84 delete static_cast<T *
>(pthread_getspecific(m_key));
85 int ret = pthread_key_delete(m_key);
91 delete static_cast<T *
>(var);
95 void *p = pthread_getspecific(m_key);
97 int ret = pthread_setspecific(m_key, p =
105 return *
static_cast<T*
>(p);
110 template <
typename T>