-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy paththread_utility.h
More file actions
52 lines (38 loc) · 819 Bytes
/
thread_utility.h
File metadata and controls
52 lines (38 loc) · 819 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#ifndef __THREAD_UTILITY__H__
#define __THREAD_UTILITY__H__
#include <vector>
#include <thread>
#include <mutex>
#include <condition_variable>
#include <queue>
namespace NThread {
class TThreadJoiner {
public:
explicit TThreadJoiner(std::vector<std::thread>& threads) : Threads_(threads) {}
~TThreadJoiner() {
Wait();
}
void Wait() {
for (auto& thread : Threads_) {
if (thread.joinable()) {
thread.join();
}
}
}
private:
std::vector<std::thread>& Threads_;
};
template <typename T>
class TThreadSafeQueue {
public:
TThreadSafeQueue() = default;
TThreadSafeQueue(const TThreadSafeQueue& other);
void Push(T value);
bool TryPop(T& value);
private:
mutable std::mutex Mutex_;
std::queue<T> Queue_;
};
} // NThread
#include "thread_utility_impl.h"
#endif