DUNE-DAQ
DUNE Trigger and Data Acquisition software
Loading...
Searching...
No Matches
SingletonCreator.hpp
Go to the documentation of this file.
1#ifndef ERS_SINGLETON_CREATOR_H
2#define ERS_SINGLETON_CREATOR_H
3
9#include <mutex>
10
11/*
12 * SingletonCreator.h
13 * ers
14 *
15 * Created by Serguei Kolos on 25.08.11.
16 * Copyright 2011 CERN. All rights reserved.
17 * This class creates singleton instance at the first use attempt
18 * in a threading-safe way.
19 */
20namespace ers
21{
22 template <class T>
23 class SingletonCreator
24 {
25 static std::mutex s_mutex;
26 static T * s_instance;
27
28 public:
29 static T * create( )
30 {
31 // on MacOS with gcc 4.2 the s_mutex is not yet created
32 // at this moment which causes crash. This might be fixed
33 // when movining to newer gcc version
34 #if !defined( __APPLE__) && !defined(__rtems__)
35 std::scoped_lock lock(s_mutex);
36 #endif
37 if ( !s_instance )
38 {
39 s_instance = new T();
40 }
41 return s_instance;
42 }
43 };
44
45 template <class T>
47
48 template <class T>
50}
51
52#endif
static std::mutex s_mutex