activemq-cpp-3.6.0
decaf::lang::ThreadLocal< E > Class Template Reference

This class provides thread-local variables. More...

#include <src/main/decaf/lang/ThreadLocal.h>

Inheritance diagram for decaf::lang::ThreadLocal< E >:

Public Member Functions

 ThreadLocal ()
 Creates a new instance of a ThreadLocal.
virtual ~ThreadLocal ()
E & get ()
 Returns the value in the current thread's copy of this thread-local variable.
void set (const E &value)
 Sets the current thread's copy of this thread-local variable to the specified value.
void remove ()
 Removes the current thread's value for this thread-local variable.

Protected Member Functions

virtual E initialValue () const
 Returns the current thread's "initial value" for this thread-local variable.
virtual void doDelete (void *value)
 Called to destroy the value held by the current thread or by the library on shutdown if there are still ThreadLocalImpl instances that have assigned TLS slots.
- Protected Member Functions inherited from decaf::internal::util::concurrent::ThreadLocalImpl
 ThreadLocalImpl ()
virtual ~ThreadLocalImpl ()
void * getRawValue () const
 Returns the current threads assigned value, but retains ownership to this value unless the remove method is subsequently called.
void setRawValue (void *value)
 Sets the raw void* value for the current thread.
void removeAll ()
 Removes from all threads any allocated data stored for this ThreadLocal instance.

Detailed Description

template<typename E>
class decaf::lang::ThreadLocal< E >

This class provides thread-local variables.

These variables differ from their normal counterparts in that each thread that accesses one (via its get or set method) has its own, independently initialized copy of the variable. ThreadLocal instances are typically private static fields in classes that wish to associate state with a thread (e.g., a user ID or Transaction ID). This class imposes the restriction on the type that it will contains that it must by both assignable and copyable.

Each thread holds an implicit reference to its copy of a thread-local variable as long as the thread is alive and the ThreadLocal instance is accessible; after a thread goes away, all of its copies of thread-local instances are destroyed.

Since
1.0

Constructor & Destructor Documentation

template<typename E >
decaf::lang::ThreadLocal< E >::ThreadLocal ( )
inline

Creates a new instance of a ThreadLocal.

template<typename E >
virtual decaf::lang::ThreadLocal< E >::~ThreadLocal ( )
inlinevirtual

Member Function Documentation

template<typename E >
virtual void decaf::lang::ThreadLocal< E >::doDelete ( void *  value)
inlineprotectedvirtual

Called to destroy the value held by the current thread or by the library on shutdown if there are still ThreadLocalImpl instances that have assigned TLS slots.

Its up to the implementor if this interface to ensure that the value held in the void* is cleaned up correctly.

Parameters
valueThe value to be destroyed for the current thread.

Implements decaf::internal::util::concurrent::ThreadLocalImpl.

template<typename E >
E& decaf::lang::ThreadLocal< E >::get ( )
inline

Returns the value in the current thread's copy of this thread-local variable.

If the variable has no value for the current thread, it is first initialized to the value returned by an invocation of the initialValue() method.

Returns
the current thread's value for this thread local.

References decaf::internal::util::concurrent::ThreadLocalImpl::getRawValue(), decaf::lang::ThreadLocal< E >::initialValue(), NULL, and decaf::internal::util::concurrent::ThreadLocalImpl::setRawValue().

template<typename E >
virtual E decaf::lang::ThreadLocal< E >::initialValue ( ) const
inlineprotectedvirtual

Returns the current thread's "initial value" for this thread-local variable.

This method will be invoked the first time a thread accesses the variable with the get() method, unless the thread previously invoked the set() method, in which case the initialValue method will not be invoked for the thread. Normally, this method is invoked at most once per thread, but it may be invoked again in case of subsequent invocations of remove() followed by get().

This implementation simply returns E(); if the programmer desires thread-local variables to have an initial value other than E(), ThreadLocal must be subclassed, and this method overridden. Typically, an inner class will be used.

Parameters
valuePointer to the thread local value created by this thread when get() is first called.

Referenced by decaf::lang::ThreadLocal< E >::get().

template<typename E >
void decaf::lang::ThreadLocal< E >::remove ( )
inline

Removes the current thread's value for this thread-local variable.

If this thread-local variable is subsequently read by the current thread, its value will be reinitialized by invoking its initialValue() method, unless its value is set by the current thread in the interim. This may result in multiple invocations of the initialValue method in the current thread.

References NULL, and decaf::internal::util::concurrent::ThreadLocalImpl::setRawValue().

template<typename E >
void decaf::lang::ThreadLocal< E >::set ( const E &  value)
inline

Sets the current thread's copy of this thread-local variable to the specified value.

Most subclasses will have no need to override this method, relying solely on the initialValue() method to set the values of thread-locals.

Parameters
valueThe new value to assign to this Thread local.

References decaf::internal::util::concurrent::ThreadLocalImpl::setRawValue().


The documentation for this class was generated from the following file: