1 #ifndef SimTK_SimTKCOMMON_THREAD_LOCAL_H_
2 #define SimTK_SimTKCOMMON_THREAD_LOCAL_H_
35 static pthread_mutex_t
keyLock = PTHREAD_MUTEX_INITIALIZER;
42 T* t =
reinterpret_cast<T*
>(value);
89 ThreadLocal(
const T& defaultValue) : defaultValue(defaultValue) {
96 pthread_key_delete(key);
103 for (std::set<void*>::const_iterator iter = instances.begin(); iter != instances.end(); ++iter) {
108 pthread_mutex_unlock(&
keyLock);
114 T* value =
reinterpret_cast<T*
>(pthread_getspecific(key));
116 return createValue();
122 const T&
get()
const {
123 T* value =
reinterpret_cast<T*
>(pthread_getspecific(key));
125 return createValue();
130 pthread_key_create(&key, cleanUpThreadLocalStorage<T>);
133 pthread_mutex_unlock(&
keyLock);
135 T& createValue()
const {
136 T* value =
new T(defaultValue);
137 pthread_setspecific(key, value);
141 pthread_mutex_unlock(&
keyLock);
150 #endif // SimTK_SimTKCOMMON_THREAD_LOCAL_H_