56 class singleton_instance_base {
58 virtual ~singleton_instance_base() {};
59 virtual int level()
const = 0;
62 class singletons_manager {
65 static singletons_manager& manager();
68 static void register_new_singleton(singleton_instance_base *p);
69 static void register_new_singleton(singleton_instance_base *p,
71 static void on_partitions_change();
74 ~singletons_manager();
80 template <
typename T,
int LEV>
81 class singleton_instance :
public singleton_instance_base {
90 static T*& instance_pointer() {
91 return pointer()->thrd_cast();
94 static T*& instance_pointer(
size_t ithread) {
95 return (*pointer())(ithread);
101 inline static T& instance(
size_t ithread) {
102 pointer()->on_thread_update();
103 T*& tinstance_ = instance_pointer(ithread);
105 tinstance_ =
new T();
106 singletons_manager::register_new_singleton(
107 new singleton_instance<T,LEV>(), ithread);
109 return *instance_pointer(ithread);
113 inline static T& instance() {
114 return instance(this_thread());
118 return pointer()->num_threads();
122 return pointer()->this_thread();
125 int level()
const override {
129 ~singleton_instance() {
130 if (!pointer())
return;
131 for(
size_t i = 0; i != pointer()->num_threads(); ++i) {
132 auto &p_singleton = (*pointer())(i);
135 p_singleton =
nullptr;
140 if (initializing_pointer) initializing_pointer =
nullptr;
145 singleton_instance<T, LEV>::initializing_pointer = singleton_instance<T, LEV>::pointer();
166 return singleton_instance<T,LEV>::instance();
169 inline static const T& const_instance() {
173 inline static T&
instance(
size_t ithread) {
174 return singleton_instance<T,LEV>::instance(ithread);
177 inline static const T& const_instance(
size_t ithread){
183 return singleton_instance<T,LEV>::num_threads();
188 return singleton_instance<T, LEV>::this_thread();
static size_type this_thread()
this thread number according to the threading policy of the singleton
static T & instance()
Instance from the current thread.
static size_type num_threads()
number of threads this singleton is distributed on.
Use this template class for any object you want to distribute to open_MP threads.
Tools for multithreaded, OpenMP and Boost based parallelization.
size_t size_type
used as the common size type in the library