Boost logo

Boost :

From: Hoeffner, Detlef (Detlef.Hoeffner_at_[hidden])
Date: 2002-04-03 06:59:01


Hello all,

I am replacing my thread abstraction with the thread package from boost. It
is very nice but I am missing two things.

The first is the possibility to get an identifier for the current thread, a
thread id.

The second is support for thread locals that behave more similar to global
or static variables than thread_specific_ptr does.
Below I have provided such a class. It provides an interface so that it can
be used as a global thread local variable. This
seems to be easier to use than the existing thread_specific_ptr and I
suggest to extend tss.hpp with such a class.

template <typename T>
class thread_local : private noncopyable
{
public:
    thread_local( const T& initvalue = T() ) : m_tss( &cleanup ),
m_initvalue( initvalue ) {}
    T& operator=( const T& value ) { return get() = value; }
    T& get() { T* p = static_cast<T*>( m_tss.get() ); if( p == 0 )
m_tss.set( p = new T( m_initvalue ) ); return *p; }
    operator T&() { return get(); }
private:
    static void cleanup(void* p) { delete static_cast<T*>(p); }

    detail::tss m_tss;
    T m_initvalue;
};

Regards

Detlef

--------------------------------------------------------------
Dipl. Wirtsch.-Ing. Detlef Höffner
c/o Commerzbank AG
Tel. +49 (0) 69 136 26845
e-mail: Detlef.Hoeffner_at_[hidden]

Tel. +49 (0) 177 3958509
e-mail: Detlef.Hoeffner_at_[hidden]
 <<Detlef Höffner (E-mail).vcf>>




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk