BmnRoot
Loading...
Searching...
No Matches
BmnAbstractThread.h
Go to the documentation of this file.
1#ifndef BMNABSTRACTTHREAD_H
2#define BMNABSTRACTTHREAD_H 1
3
4#include <chrono>
5#include <condition_variable>
6#include <iostream>
7#include <mutex>
8#include <queue>
9#include <string>
10
11// #include "Rtypes.h"
12// #include "TStopwatch.h"
13
14#pragma GCC system_header
15
16using CClock = std::chrono::steady_clock;
17using CPoint = std::chrono::time_point<CClock>;
18using CDuration = std::chrono::duration<double>;
19
21{
22 public:
23 void Run()
24 {
25 if (calcStatistics) {
26 std::lock_guard<std::mutex> statLock(statVarAccess);
27 curThreadCount++;
28 }
29 std::unique_lock<std::mutex> dataLock(dataAccess);
30 while (true) {
31 if (calcStatistics) {
32 std::lock_guard<std::mutex> statLock(statVarAccess);
33 curThreadWaiting++;
34 }
35 if (measureTime) { // timer->Start();
36 ctime = CClock::now();
37 }
38 proceed.wait(dataLock, [this] { return dataReady; });
39 if (calcStatistics) {
40 std::lock_guard<std::mutex> statLock(statVarAccess);
41 curThreadWaiting--;
42 }
43 if (measureTime) {
44 // timer->Stop();
45 // time1 += timer->RealTime();
46 ctime1 += CClock::now() - ctime;
47 }
48 if (taskId == -1) {
49 dataReady = false;
50 break;
51 }
52 if (measureTime) { // timer->Start();
53 ctime = CClock::now();
54 }
55
56 Calculate();
57
58 if (measureTime) {
59 // timer->Stop();
60 // time2 += timer->RealTime();
61 ctime2 += CClock::now() - ctime;
62 }
63 // adding this thread back to the queue
64 dataReady = false;
65 std::lock_guard<std::mutex> queueLock(*queueAccess);
66 threadQueue->push(threadId);
67 queueWait->notify_one();
68 }
69 if (calcStatistics) {
70 std::lock_guard<std::mutex> statLock(statVarAccess);
71 curThreadCount--;
72 }
73 }
74
76 {
77 if (calcStatistics)
78 return curThreadCount;
79 else
80 return -1;
81 }
82
84 {
85 if (calcStatistics)
86 return curThreadWaiting;
87 else
88 return -1;
89 }
90
91 // double GetWaitingTime(){
92 // if (measureTime) return time1;
93 // else return -1;
94 // }
95 //
96 // double GetCalculationTime(){
97 // if (measureTime) return time2;
98 // else return -1;
99 // }
100
101 CDuration GetWaitingCTime() { return ctime1; }
102
103 CDuration GetCalculationCTime() { return ctime2; }
104
105 int32_t GetId() { return threadId; }
106
107 virtual void Terminate()
108 {
109 std::cout << "Abstract method is called!\n";
110 } // only call if the thread is not calculating
111
112 void Execute()
113 {
114 std::lock_guard<std::mutex> dataLock(dataAccess);
115 dataReady = true;
116 proceed.notify_one();
117 }
118
120 {
121 std::lock_guard<std::mutex> queueLock(*queueAccess);
122 threadQueue->push(threadId);
123 queueWait->notify_one();
124 }
125
126 virtual void SetInitData(...) { std::cout << "Abstract method is called!\n"; }
127 virtual void SetData(...) { std::cout << "Abstract method is called!\n"; }
128
129 protected:
130 BmnAbstractThread(std::queue<int32_t>* threadQueue,
131 std::mutex* queueAccess,
132 std::condition_variable* queueWait,
133 int32_t id,
134 bool measureTime = false)
135 : threadQueue(threadQueue)
136 , queueAccess(queueAccess)
137 , queueWait(queueWait)
138 , dataReady(false)
139 , threadId(id)
140 , taskId(-2)
141 , measureTime(measureTime) //, time1(0.0), time2(0.0)
142 {
143 // timer = new TStopwatch();
144 }
145
147 {
148 // delete timer;
149 }
150
151 int32_t threadId; // this thread id
152 int32_t taskId; // current task id
153
154 virtual void Calculate() { std::cout << "Abstract method is called!\n"; }
155
156 private:
157 // variables for keeping track of current threads
158 static const bool calcStatistics;
159 static std::mutex statVarAccess;
160 static int32_t curThreadWaiting;
161 static int32_t curThreadCount;
162
163 const bool measureTime; // tells whether to use the timer
164 // TStopwatch* timer; //timer for evaluating performance
165 CPoint ctime;
166 // double time1, time2; //variables to keep track of the performance 1-waiting time, 2-calculation time
167 CDuration ctime1, ctime2; // variables to keep track of the performance 1-waiting time, 2-calculation time
168
169 std::condition_variable* queueWait; // makes the ThreadManager class wait in case the queue is empty
170 std::queue<int32_t>* threadQueue; // queue from the ThreadManager class
171 std::mutex* queueAccess; // access to read/write into the queue
172
173 std::condition_variable proceed; // makes the thread wait untill its data is set
174 std::mutex dataAccess; // mutex for accessing the data of this thread
175 bool dataReady;
176
177 // ClassDef(BmnAbstractThread, 1);
178};
179
180#endif
virtual void SetInitData(...)
virtual void SetData(...)
virtual void Terminate()
CDuration GetCalculationCTime()
virtual void Calculate()
BmnAbstractThread(std::queue< int32_t > *threadQueue, std::mutex *queueAccess, std::condition_variable *queueWait, int32_t id, bool measureTime=false)
CDuration GetWaitingCTime()
std::chrono::duration< double > CDuration
std::chrono::time_point< CClock > CPoint
std::chrono::steady_clock CClock
std::chrono::duration< double > CDuration