Boost logo

Boost :

From: William Kempf (williamkempf_at_[hidden])
Date: 2002-04-03 09:57:28


>From: "Hoeffner, Detlef" <Detlef.Hoeffner_at_[hidden]>
>Reply-To: boost_at_[hidden]
>To: "'boost_at_[hidden]'" <boost_at_[hidden]>
>Subject: [boost] Thread locals
>Date: Wed, 3 Apr 2002 13:59:01 +0200
>
>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.

Not an easy thing to do portably (at least to my liking). However, I have
plans to add this very soon... though the id won't be a native integral
type.

>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;
>};

This has a few warts, such as the type having to support copy construction,
no way to use the stack instead of dynamic store (though, admittedly, doing
this with thread_local_ptr<> is a bit of a hack, even if specifically
designed for), etc. However, I understand the convenience of this and so
I'll add something soon. Thanks for the suggestion.

Bill Kempf
williamkempf_at_[hidden]

_________________________________________________________________
Join the world’s largest e-mail service with MSN Hotmail.
http://www.hotmail.com


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