class TSS public todo defeat memory leak current scheme doesn work des

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
class TSS
{
public:
/**
@todo defeat memory leak: current scheme doesn't work, 'destructor' will never be called.
*/
TSS(void (*destructor)(void *) = 0) : key_(0)
{
if (::pthread_key_create(&key_, destructor) != 0)
{ throw std::runtime_error("TSS error"); }
}
~TSS() throw()
{ (void) ::pthread_key_delete(key_); }
void * Get() const
{ return ::pthread_getspecific(key_); }
bool Set(void * newValue)
{ return (::pthread_setspecific(key_, newValue) == 0); }
private:
pthread_key_t key_;
}; // class TSS