Boost logo

Boost :

From: Hoeffner, Detlef (Detlef.Hoeffner_at_[hidden])
Date: 2002-04-04 12:25:17


Her is an optimized version of thread_local that places values directly in
the void* if it fits
and otherwise allocates place on the heap.

There is still one thing missing: The initilaization of the initvalue in the
case that it fits
in the pointer. This requires an additional hook when the TlsValue is
created initially.

template <bool fitsInVoidPtr>
struct thread_local_base
{
   template <typename T>
   class Type
   {
   public:
       Type( const T& initvalue = T() ) : m_tss( &cleanup ), m_initvalue(
initvalue ) {}
       void set( T val ) { m_tss.set( reinterpret_cast<void*>( val ) ); }
       T get() { void* p = m_tss.get(); return *reinterpret_cast<T*>( &p );
}
       operator T() { return get(); }
   private:
       static void cleanup(void* p) { }

       detail::tss m_tss;
       T m_initvalue;
   };
};

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

       detail::tss m_tss;
       T m_initvalue;
   };
};

template <typename T>
class thread_local : public
thread_local_base<sizeof(T)<=sizeof(void*)>::Type<T>
{
public:
    typedef thread_local_base<sizeof(T)<=sizeof(void*)>::Type<T> base;
    thread_local( const T& initvalue = T() ) : base( initvalue ) {}
    bool operator==( T val ) { return get() == val; }
    thread_local& operator=( const T& value ) { set( value ); return *this;
}
};

William, I have a question regarding your statement about the representation
of pthread_t.

On which platform isn't this printable?

Regards

Detlef

-----Original Message-----
From: William Kempf [mailto:williamkempf_at_[hidden]]
Sent: Donnerstag, 4. April 2002 17:07
To: boost_at_[hidden]
Subject: RE: [boost] Thread locals

>From: "Hoeffner, Detlef" <Detlef.Hoeffner_at_[hidden]>
>Reply-To: boost_at_[hidden]
>To: "'boost_at_[hidden]'" <boost_at_[hidden]>
>Subject: RE: [boost] Thread locals
>Date: Wed, 3 Apr 2002 18:53:55 +0200
>
>Hi William,
>
> > However, I understand the convenience of this and so
> > I'll add something soon. Thanks for the suggestion.
>
>Thanks!
>
> > The only thing missing in boost::thread from your list is
> > operator<<(ostream&, ).
>
>I still do not see how I would identify the current instance of thread.
>Am I missing something?

The default constructor creates a boost::thread for the current thread, and
boost::thread has == and != operators that can then be used for the
comparison. Again, the only thing missing from your list was a way to
output and identifying textual representation. Though to really be useful
we also need a copyable form of ID.

> > I'm going to work around this by using a TSS pointer value,
> > which seems to be the best solution, though it's not a perfect solution.
>
>I guess for performance issues it would be preferable to use functions the
>operating system or the thread library provides, e.g.
>
> static thread_id getCurrentThreadId()
> {
>#ifdef POSIX_THREADS
> return pthread_self();
>#elif WIN32_THREADS
> return GetCurrentThreadId();
>#endif
> }

The reasons to not do this:

1. It doesn't provide a means for outputting a textual representation.

2. It doesn't provide LessThanComparable semantics, so it can't be used as a

key in a set/map, which isn't in your list of needs but is still something I

think we need.

Bill Kempf
williamkempf_at_[hidden]

_________________________________________________________________
MSN Photos is the easiest way to share and print your photos:
http://photos.msn.com/support/worldwide.aspx

_______________________________________________
Unsubscribe & other changes:
http://lists.boost.org/mailman/listinfo.cgi/boost


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