-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy paththread_group.h
54 lines (42 loc) · 892 Bytes
/
thread_group.h
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
53
54
/*
* thread_group.h
* Created on: 2021-7-30
* Author: qianqians
* thread_group
*/
#ifndef _THREAD_GROUP_H
#define _THREAD_GROUP_H
#include <memory>
#include <vector>
#include <thread>
#include <functional>
#include <mutex>
namespace concurrent {
class thread_group {
private:
std::mutex _l_th_mu;
std::vector<std::shared_ptr<std::jthread> > _th_group;
public:
thread_group() {}
virtual ~thread_group() {
_th_group.clear();
}
std::shared_ptr<std::jthread> create_thread(std::function<void()> fn) {
auto th = std::make_shared<std::jthread>(fn);
std::lock_guard<std::mutex> l(_l_th_mu);
_th_group.emplace_back(th);
return th;
}
void join_all() {
std::lock_guard<std::mutex> l(_l_th_mu);
for (auto th : _th_group) {
th->join();
}
_th_group.clear();
}
size_t size() {
return _th_group.size();
}
};
} /* concurrent */
#endif //_THREAD_GROUP_H