Scarab  v3.5.1
Project 8 C++ Utility Library
Static Public Member Functions | Protected Member Functions | Static Protected Attributes | Static Private Member Functions | Static Private Attributes | Friends | List of all members
thread_singleton< x_type > Class Template Reference

Base class that turns a class into a thread-local singleton. More...

#include <thread_singleton.hh>

Static Public Member Functions

static x_type * get_instance ()
 
static void kill_instance ()
 
template<class... x_args>
static x_type * create_instance (x_args... args)
 

Protected Member Functions

 thread_singleton ()
 
 ~thread_singleton ()
 

Static Protected Attributes

static thread_local std::mutex f_mutex
 

Static Private Member Functions

static void construct_instance ()
 
static void delete_instance ()
 

Static Private Attributes

static thread_local x_type * f_instance = nullptr
 
static thread_local destroyer< x_type > f_destroyer
 

Friends

class destroyer< x_type >
 

Detailed Description

template<class x_type>
class scarab::thread_singleton< x_type >

Base class that turns a class into a thread-local singleton.

Author
N.S. Oblath

A thread-local singleton is similar to a singleton except that a different instance exists in each thread.

To use:

  1. Inherit your class from thread_singleton< your_class >
  2. Make your constructor and destructor protected (or private)
  3. Add the allow_thread_singleton_access( your_class ) macro to your class definition to allow the base classes to access your class

The mutex f_mutex provides thread safety for creation and destruction of an instance of the thread_singleton. It's available to the derived class for thread-safe access to the object in general.

Definition at line 45 of file thread_singleton.hh.

Constructor & Destructor Documentation

◆ thread_singleton()

thread_singleton ( )
protected

Definition at line 139 of file thread_singleton.hh.

◆ ~thread_singleton()

~thread_singleton ( )
protected

Definition at line 143 of file thread_singleton.hh.

Member Function Documentation

◆ construct_instance()

void construct_instance ( )
staticprivate

Definition at line 118 of file thread_singleton.hh.

◆ create_instance()

x_type * create_instance ( x_args...  args)
static

Definition at line 105 of file thread_singleton.hh.

◆ delete_instance()

void delete_instance ( )
staticprivate

Definition at line 128 of file thread_singleton.hh.

◆ get_instance()

x_type * get_instance ( )
static

Definition at line 82 of file thread_singleton.hh.

◆ kill_instance()

void kill_instance ( )
static

Definition at line 93 of file thread_singleton.hh.

Friends And Related Function Documentation

◆ destroyer< x_type >

friend class destroyer< x_type >
friend

Definition at line 68 of file thread_singleton.hh.

Member Data Documentation

◆ f_destroyer

thread_local destroyer< x_type > f_destroyer
staticprivate

Definition at line 60 of file thread_singleton.hh.

◆ f_instance

thread_local x_type * f_instance = nullptr
staticprivate

Definition at line 59 of file thread_singleton.hh.

◆ f_mutex

thread_local std::mutex f_mutex
staticprotected

Definition at line 63 of file thread_singleton.hh.


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