Logo ROOT   6.16/01
Reference Guide
TPoolManager.cxx
Go to the documentation of this file.
2#include "TError.h"
3#include "TROOT.h"
4#include <algorithm>
5#include "tbb/task_scheduler_init.h"
6
7namespace ROOT {
8
9 namespace Internal {
10 //Returns the weak_ptr reflecting a shared_ptr to the only instance of the Pool Manager.
11 //This will allow to check if the shared_ptr is still alive, solving the dangling pointer problem.
12 std::weak_ptr<TPoolManager> &GetWP()
13 {
14 static std::weak_ptr<TPoolManager> weak_sched;
15 return weak_sched;
16 }
17
19
20 TPoolManager::TPoolManager(UInt_t nThreads): fSched(new tbb::task_scheduler_init(tbb::task_scheduler_init::deferred))
21 {
22 //Is it there another instance of the tbb scheduler running?
23 if (fSched->is_active()) {
24 mustDelete = false;
25 }
26
27 nThreads = nThreads != 0 ? nThreads : tbb::task_scheduler_init::default_num_threads();
28 fSched ->initialize(nThreads);
29 fgPoolSize = nThreads;
30 };
31
33 {
34 //Only terminate the tbb scheduler if there was not another instance already
35 // running when the constructor was called.
36 if (mustDelete) {
37 fSched->terminate();
38 fgPoolSize = 0;
39 }
40 }
41
42 //Number of threads the PoolManager has been initialized with.
44 {
45 return fgPoolSize;
46 }
47
48 //Factory function returning a shared pointer to the only instance of the PoolManager.
49 std::shared_ptr<TPoolManager> GetPoolManager(UInt_t nThreads)
50 {
51 if (GetWP().expired()) {
52 std::shared_ptr<TPoolManager> shared(new TPoolManager(nThreads));
53 GetWP() = shared;
54 return GetWP().lock();
55 }
56 return GetWP().lock();
57 }
58 }
59}
unsigned int UInt_t
Definition: RtypesCore.h:42
TPoolManager(UInt_t nThreads=0)
Initializes the scheduler within ROOT.
tbb::task_scheduler_init * fSched
~TPoolManager()
Terminates the scheduler instantiated within ROOT.
static UInt_t GetPoolSize()
Returns the number of threads running when the scheduler has been instantiated within ROOT.
std::shared_ptr< TPoolManager > GetPoolManager(UInt_t nThreads=0)
Get a shared pointer to the manager.
std::weak_ptr< TPoolManager > & GetWP()
Namespace for new ROOT classes and functions.
Definition: StringConv.hxx:21