Ion
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ion::base::ThreadLocalObject< T > Class Template Reference

This templated class makes it easy to create an instance of an object in thread-local storage. More...

#include "threadlocalobject.h"

Inheritance diagram for ion::base::ThreadLocalObject< T >:

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::ThreadLocalStorageKeyGetKey () const
 Returns the ThreadLocalStorageKey created by the instance. More...
 
TGet ()
 Returns a T instance for the current thread, creating it first if necessary. More...
 

Detailed Description

template<typename T>
class ion::base::ThreadLocalObject< T >

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.

Constructor & Destructor Documentation

template<typename T>
ion::base::ThreadLocalObject< T >::ThreadLocalObject ( )
inline

The default constructor will use global operator new() to construct T instances.

Definition at line 75 of file threadlocalobject.h.

template<typename T>
ion::base::ThreadLocalObject< T >::ThreadLocalObject ( const AllocatorPtr allocator)
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.

template<typename T>
ion::base::ThreadLocalObject< T >::~ThreadLocalObject ( )
inline

Definition at line 84 of file threadlocalobject.h.

Member Function Documentation

template<typename T>
T* ion::base::ThreadLocalObject< T >::Get ( )
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().

template<typename T>
const port::ThreadLocalStorageKey& ion::base::ThreadLocalObject< T >::GetKey ( ) const
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.


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