mn_atomic_singleton.hpp
Go to the documentation of this file.
1 
20 #ifndef __MINLIB_ATOMIC_SINGLETON_H__
21 #define __MINLIB_ATOMIC_SINGLETON_H__
22 
23 #include "mn_config.hpp"
24 
25 #include "mn_atomic.hpp"
26 #include "mn_autolock.hpp"
27 #include "mn_copyable.hpp"
28 
29 
30 namespace mn {
31 
36  template < typename T>
38  friend class T;
39  public:
40  using value_type = T;
42  using pointer = T*;
44 
45  basic_atomic_singleton(self_type&& other) = delete;
47 
48  static reference instance() {
50  if(!_tmp) {
52 
53  _tmp = m_pInstance.load (memory_order::Consume);
54  if( !_tmp ) {
55  _tmp = new T();
56  m_pInstance.store (_tmp, memory_order::Release);
57  }
58  }
59  return tmp;
60  }
61  private:
64  };
65 
66  template < typename T >
68 
69  template < typename T >
71 
72  template <typename T>
74 }
75 
76 
77 #endif
Singleton with double-checked locking pattern with atomic and mutex lock.
Definition: mn_atomic_singleton.hpp:37
static mn::mutex_t m_tLock
Definition: mn_atomic_singleton.hpp:63
basic_atomic_singleton(const self_type &)=delete
static reference instance()
Definition: mn_atomic_singleton.hpp:48
basic_atomic_singleton(self_type &&other)=delete
value_type & reference
Definition: mn_atomic_singleton.hpp:41
static mn::_atomic< pointer > m_pInstance
Definition: mn_atomic_singleton.hpp:62
T * pointer
Definition: mn_atomic_singleton.hpp:42
friend class T
Definition: mn_atomic_singleton.hpp:38
T value_type
Definition: mn_atomic_singleton.hpp:40
Definition: mn_lock.hpp:122
Definition: mn_mutex.hpp:37
Basic atomics types This file is part of the Mini Thread Library (https://github.com/RoseLeBlood/Mini...
Definition: mn_allocator_typetraits.hpp:25
int lock(TLOCK &m1, unsigned int timeout)
Definition: mn_autolock.hpp:70