-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrunners.h
39 lines (34 loc) · 1.07 KB
/
runners.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
#pragma once
#include <atomic>
#include <functional>
#include <memory>
#include <thread>
namespace utility {
using runnerint_t = std::shared_ptr<std::atomic<bool>>;
using runner_f_t = std::function<void(const runnerint_t should_int)>;
// simple way to execute lambda in thread, in case when shared_ptr is cleared it will send
// stop notify and join(), so I can ensure 1 pointer has only 1 running thread always for the same
// task
inline auto startNewRunner(runner_f_t func)
{
using res_t = std::shared_ptr<std::thread>;
const auto stop = runnerint_t(new std::atomic<bool>(false));
const auto threadException =
std::shared_ptr<std::exception_ptr>(new std::exception_ptr(nullptr));
return res_t(new std::thread(func, stop), [stop, threadException](auto p) {
stop->store(true);
if (p)
{
if (p->joinable())
{
p->join();
}
delete p;
}
});
}
inline size_t currentThreadId()
{
return std::hash<std::thread::id>{}(std::this_thread::get_id());
}
} // namespace utility