-
Notifications
You must be signed in to change notification settings - Fork 158
/
Copy pathThreadManager.cpp
63 lines (56 loc) · 2.07 KB
/
ThreadManager.cpp
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
55
56
57
58
59
60
61
62
63
#include "internal/ThreadManager.h"
#include <assert.h>
#include <algorithm>
SL::Screen_Capture::ThreadManager::ThreadManager()
{
}
SL::Screen_Capture::ThreadManager::~ThreadManager()
{
Join();
}
void SL::Screen_Capture::ThreadManager::Init(const std::shared_ptr<Thread_Data>& data)
{
assert(m_ThreadHandles.empty());
if (data->ScreenCaptureData.getThingsToWatch) {
auto monitors = data->ScreenCaptureData.getThingsToWatch();
auto mons = GetMonitors();
for ([[maybe_unused]] auto &m : monitors) {
assert(isMonitorInsideBounds(mons, m));
}
m_ThreadHandles.resize(monitors.size() + (data->ScreenCaptureData.OnMouseChanged ? 1 : 0)); // add another thread for mouse capturing if needed
for (size_t i = 0; i < monitors.size(); ++i) {
m_ThreadHandles[i] = std::thread(&SL::Screen_Capture::RunCaptureMonitor, data, monitors[i]);
}
if (data->ScreenCaptureData.OnMouseChanged) {
m_ThreadHandles.back() = std::thread([data] {
SL::Screen_Capture::RunCaptureMouse(data);
});
}
}
else if (data->WindowCaptureData.getThingsToWatch) {
auto windows = data->WindowCaptureData.getThingsToWatch();
m_ThreadHandles.resize(windows.size() + (data->WindowCaptureData.OnMouseChanged ? 1 : 0)); // add another thread for mouse capturing if needed
for (size_t i = 0; i < windows.size(); ++i) {
m_ThreadHandles[i] = std::thread(&SL::Screen_Capture::RunCaptureWindow, data, windows[i]);
}
if (data->WindowCaptureData.OnMouseChanged) {
m_ThreadHandles.back() = std::thread([data] {
SL::Screen_Capture::RunCaptureMouse(data);
});
}
}
}
void SL::Screen_Capture::ThreadManager::Join()
{
for (auto& t : m_ThreadHandles) {
if (t.joinable()) {
if (t.get_id() == std::this_thread::get_id()) {
t.detach();// will run to completion
}
else {
t.join();
}
}
}
m_ThreadHandles.clear();
}