summaryrefslogtreecommitdiffstats
path: root/raul/ThreadVar.hpp
diff options
context:
space:
mode:
Diffstat (limited to 'raul/ThreadVar.hpp')
-rw-r--r--raul/ThreadVar.hpp10
1 files changed, 6 insertions, 4 deletions
diff --git a/raul/ThreadVar.hpp b/raul/ThreadVar.hpp
index 54e0d4e..eb6b63e 100644
--- a/raul/ThreadVar.hpp
+++ b/raul/ThreadVar.hpp
@@ -17,7 +17,6 @@
#ifndef RAUL_THREADVAR_HPP
#define RAUL_THREADVAR_HPP
-#include <stdlib.h>
#include <pthread.h>
namespace Raul {
@@ -33,7 +32,7 @@ public:
ThreadVar(const T& default_value)
: _default_value(default_value)
{
- pthread_key_create(&_key, free);
+ pthread_key_create(&_key, destroy_value);
}
~ThreadVar() {
@@ -45,8 +44,7 @@ public:
if (val) {
*val = value;
} else {
- val = (T*)malloc(sizeof(value));
- *val = value;
+ val = new T(value);
pthread_setspecific(_key, val);
}
return *this;
@@ -61,6 +59,10 @@ private:
ThreadVar(const ThreadVar& noncopyable);
ThreadVar& operator=(const ThreadVar& noncopyable);
+ static void destroy_value(void* ptr) {
+ delete (T*)ptr;
+ }
+
const T _default_value;
pthread_key_t _key;
};