Rosetta  2019.07
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Functions
utility::thread Namespace Reference

Classes

class  SharedThreadLocalData
 

Functions

template<class T >
void safely_create_singleton (boost::function< T *() > creation_func, T *&instance)
 Safely instantiate a singleton class in a (possibly) multithreaded context. More...
 
template<class T >
bool safely_determine_whether_singleton_exists (T *const &instance)
 Given a pointer, determine whether the singleton to which it points has already been created. More...
 
template<class T >
void safely_create_load_once_object_by_OP (boost::function< utility::pointer::shared_ptr< T >() > creation_func, utility::pointer::shared_ptr< T > &instance, bool const , bool const )
 Safely instantiate a singleton class in a (possibly) multithreaded context. This version works with shared_ptrs. More...
 
template<class T , class K >
utility::pointer::shared_ptr< Tsafely_check_map_for_key_and_insert_if_absent (typename boost::function< utility::pointer::shared_ptr< T >() > builder, bool const , K const &tname, typename std::map< K, utility::pointer::shared_ptr< T > > &tmap)
 Check for a string map key in a map of string->owning pointers. If the key is not present, create an object by owning pointer and insert it in the map with the given key. More...
 

Function Documentation

template<class T , class K >
utility::pointer::shared_ptr< T > utility::thread::safely_check_map_for_key_and_insert_if_absent ( typename boost::function< utility::pointer::shared_ptr< T >() >  builder,
bool  const,
K const &  tname,
typename std::map< K, utility::pointer::shared_ptr< T > > &  tmap 
)

Check for a string map key in a map of string->owning pointers. If the key is not present, create an object by owning pointer and insert it in the map with the given key.

Uses a utility::thread::ReadWriteMutex to allow many threads to read/check for the key, and only one thread to write/create the object with the key. Returns the object from the map.

Author
Vikram K. Mulligan (vmull.nosp@m.ig@u.nosp@m.w.edu).
template<class T >
void utility::thread::safely_create_load_once_object_by_OP ( boost::function< utility::pointer::shared_ptr< T >() >  creation_func,
utility::pointer::shared_ptr< T > &  instance,
bool  const,
bool  const 
)
inline

Safely instantiate a singleton class in a (possibly) multithreaded context. This version works with shared_ptrs.

In the non-multithreaded case, this simply checks the singleton's instance member; in the multithreaded case, it checks the instance member, then it obtains the singleton's instance-creation mutex, then it checks the instance member again, to ensure that no other thread has already created the instance, it creates the instance, and then it releases the mutex.

Note
This function contains ifdef'd special-case logic for GCC 4.8 and 4.9, which lacked the C++11-standard functions std::atomic_load(std::shared_ptr) and std::atomic_store(std::shared_ptr). The workaround uses an std::atomic_bool for data loading checks, and should result in very little cost in performance or memory. (Thank you to Andrew Leaver-Fay for suggesting the workaround.)
Author
Vikram K. Mulligan (vmull.nosp@m.ig@u.nosp@m.w.edu) – modified from safely_create_singleton().
template<class T >
void utility::thread::safely_create_singleton ( boost::function< T *() >  creation_func,
T *&  instance 
)
inline

Safely instantiate a singleton class in a (possibly) multithreaded context.

In the non-multithreaded case, this simply checks the singleton's instance member; in the multithreaded case, it checks the instance member, then it obtains the singleton's instance-creation mutex, then it checks the instance member again, to ensure that no other thread has already created the instance, it creates the instance, and then it releases the mutex.

Requires that class T defines: static std::mutex & singleton_mutex(),

template<class T >
bool utility::thread::safely_determine_whether_singleton_exists ( T *const &  instance)
inline

Given a pointer, determine whether the singleton to which it points has already been created.

The determination is threadsafe (though there is no guarantee that the singleton won't be created by another thread immediately after this thread is done with this inquiry).

Author
Vikram K. Mulligan (vmull.nosp@m.ig@u.nosp@m.w.edu)