Ion
|
This templated class makes it easy to create an instance of an object in thread-local storage. More...
#include "threadlocalobject.h"
Public Member Functions | |
ThreadLocalObject () | |
The default constructor will use global operator new() to construct T instances. More... | |
ThreadLocalObject (const AllocatorPtr &allocator) | |
This constructor uses the given Allocator to construct T instances. More... | |
~ThreadLocalObject () | |
const port::ThreadLocalStorageKey & | GetKey () const |
Returns the ThreadLocalStorageKey created by the instance. More... | |
T * | Get () |
Returns a T instance for the current thread, creating it first if necessary. More... | |
This templated class makes it easy to create an instance of an object in thread-local storage.
It obtains and manages a TLS key that can be used in all threads and sets the TLS pointer in each thread to the object using that key.
The wrapped object must be default-constructable for this class to compile.
For example, consider a singleton "Manager" class that needs to store a unique "Info" instance per thread:
class Manager { public: ///< This returns a Info instance that is specific to the current thread. const Info& GetInfo() const { return *tl_.Get(); } ... private: ThreadLocalObject<Info> tl_; };
This example shows how a non-singleton "Thing" class can use a static ThreadLocalObject to create a different singleton "Thing::Helper" class instance per thread.
class Thing { ... private: ///< This returns a static Helper instance that is specific to the current ///< thread. Helper* GetHelper(); };
///< In the source file: Thing::Helper* Thing::GetHelper() { ION_DECLARE_SAFE_STATIC_POINTER(ThreadLocalObject<Helper>, s_helper); return s_helper->Get(); }
Definition at line 71 of file threadlocalobject.h.
|
inline |
The default constructor will use global operator new() to construct T instances.
Definition at line 75 of file threadlocalobject.h.
|
inlineexplicit |
This constructor uses the given Allocator to construct T instances.
This will compile only if T is derived from Allocatable.
Definition at line 80 of file threadlocalobject.h.
|
inline |
Definition at line 84 of file threadlocalobject.h.
|
inline |
Returns a T instance for the current thread, creating it first if necessary.
All subsequent calls on the same thread will return the same instance.
Definition at line 99 of file threadlocalobject.h.
Referenced by ion::profile::CallTraceManager::GetNamedTraceRecorder().
|
inline |
Returns the ThreadLocalStorageKey created by the instance.
This will be port::kInvalidThreadLocalStorageKey if anything went wrong.
Definition at line 94 of file threadlocalobject.h.