BmnRoot
Loading...
Searching...
No Matches
BmnThreadManager.h
Go to the documentation of this file.
1#ifndef BMNTHREADMANAGER_H
2#define BMNTHREADMANAGER_H 1
3
4#include <condition_variable>
5#include <cstdarg>
6#include <iostream>
7#include <mutex>
8#include <queue>
9#include <thread>
10#include <vector>
11// BmnRoot
12#include "BmnAbstractThread.h"
13#include "BmnConverterThread.h"
14
15#pragma GCC system_header
16
17template<typename T>
19{
20 public:
21 virtual ~BmnThreadManager() {}
22
23 T* Add(Int_t threadType = 0)
24 {
26 switch (threadType) {
27 case 0:
28 tmp = new T(&threadQueue, &queueAccess, &queueWait, threads.size(), kTRUE);
29 break;
30 }
31 threadQueue.push(threads.size());
32 threads.push_back(tmp);
33 std::thread t(&T::Run, tmp);
34 t.detach();
35 return (T*)tmp;
36 }
37
38 void Finish()
39 {
40 std::unique_lock<std::mutex> lk(queueAccess);
41 queueWait.wait(lk, [this] { return threadQueue.size() == threads.size(); });
42 }
43
44 void Terminate()
45 {
46 for (int i = 0; i < threads.size(); i++)
47 GetWaitingThread()->Terminate();
48 }
49
51 {
52 std::unique_lock<std::mutex> lk(queueAccess);
53 queueWait.wait(lk, [this] { return !threadQueue.empty(); });
54 Int_t threadId = threadQueue.front();
55 threadQueue.pop();
56 lk.unlock();
57 return (T*)threads[threadId];
58 }
59
60 T* GetThread(int index) { return (T*)threads[index]; }
61
62 private:
63 vector<BmnAbstractThread*> threads;
64 std::mutex queueAccess;
65 std::condition_variable queueWait;
66 queue<Int_t> threadQueue;
67
68 ClassDef(BmnThreadManager<T>, 1);
69};
70
71#endif
int i
Definition P4_F32vec4.h:22
T * GetThread(int index)
T * Add(Int_t threadType=0)
virtual ~BmnThreadManager()