Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef __PER_THREAD_H__
- #define __PER_THREAD_H__
- #include <assert.h>
- #include <pthread.h>
- template <typename T>
- class PerThread
- {
- public:
- PerThread()
- {
- assert(pthread_key_create(&m_oKey, destructor) == 0);
- }
- ~PerThread()
- {
- (void)pthread_key_delete(m_oKey);
- }
- T& Get()
- {
- T *value = (T*)pthread_getspecific(m_oKey);
- if (value == NULL)
- {
- value = new T();
- assert(value);
- assert(pthread_setspecific(m_oKey, value) == 0);
- }
- return *value;
- }
- private:
- static void destructor(void *value)
- {
- if (value != NULL)
- delete (T*)value;
- }
- private:
- pthread_key_t m_oKey;
- };
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement