53 class singleton_instance_base {
55 virtual ~singleton_instance_base() {}
56 virtual int level() = 0;
60 class singletons_manager {
63 static singletons_manager& manager();
64 static singletons_manager& m;
67 static void register_new_singleton(singleton_instance_base *p);
68 static void register_new_singleton(singleton_instance_base *p,
70 ~singletons_manager();
78 template <
typename T,
int LEV>
class singleton_instance
79 :
public singleton_instance_base {
86 static T*& instance_pointer() {
return omp_distro_pointer()->thrd_cast(); }
87 static T*& instance_pointer(
size_t ithread)
88 {
return (*omp_distro_pointer())(ithread);}
92 singleton_instance() {}
95 inline static T& instance() {
96 T*& tinstance_ = instance_pointer();
99 singletons_manager::register_new_singleton
100 (
new singleton_instance<T,LEV>());
106 inline static T& instance(
size_t ithread) {
107 T*& tinstance_ = instance_pointer(ithread);
109 tinstance_ =
new T();
110 singletons_manager::register_new_singleton
111 (
new singleton_instance<T,LEV>(),ithread);
116 int level() {
return LEV; }
118 ~singleton_instance() {
120 for(
size_t i = 0; i < getfem::num_threads(); i++) {
121 if((*instance_)(i)) {
122 delete (*instance_)(i);
127 delete instance_; instance_=0;
147 return singleton_instance<T,LEV>::instance();
149 inline static const T& const_instance() {
return instance(); }
151 inline static T& instance(
size_t ithread) {
152 return singleton_instance<T,LEV>::instance(ithread);
154 inline static const T& const_instance(
size_t ithread)
155 {
return instance(ithread); }
166 template <
typename T,
int LEV>
168 = singleton_instance<T,LEV>::omp_distro_pointer();
use this template class for any object you want to distribute to open_MP threads. ...
Tools for multithreaded, OpenMP and Boost based parallelization.
static T & instance()
Instance from the current thread.