-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpass_thread_runner.cpp
More file actions
70 lines (57 loc) · 1.79 KB
/
pass_thread_runner.cpp
File metadata and controls
70 lines (57 loc) · 1.79 KB
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
64
65
66
67
68
69
70
#include <functional>
#include <memory>
#include <print>
#include <scl/safe_thread.h>
/**
A wrapper that can be used around pointers to allow the this pointer to be passed.
There's nothing special about this, it's just a dump pointer but means we can
explicitly allow it when we get reflection and makes it obvious in calling
code that the intent is to share it.
*/
template<typename T>
class unchecked_pointer
{
public:
unchecked_pointer (nullptr_t) = delete;
unchecked_pointer (T* p)
: pointer (p) {
}
unchecked_pointer (T& p)
: pointer (*p) {
}
T& operator*() const {
return *pointer;
}
T* operator->() const {
return pointer;
}
private:
T* pointer;
};
template<typename T>
struct scl::is_send<unchecked_pointer<T>> : std::true_type {};
using namespace std::literals;
class thread_runner
{
public:
thread_runner()
{
std::mem_fn (&thread_runner::run) (unchecked_pointer<thread_runner> (this));
// static_assert(is_send_v<decltype(std::mem_fn (&thread_runner::run))>);
// static_assert(is_send_v<std::decay_t<decltype(std::mem_fn (&thread_runner::run))>>);
thread = std::make_unique<scl::thread> (std::mem_fn (&thread_runner::run), unchecked_pointer (this));
// thread = std::make_unique<safe_thread> (std::mem_fn (&thread_runner::run), this_wrapper<const thread_runner> (const_cast<const thread_runner*> (this)));
// thread = std::make_unique<safe_thread> (std::mem_fn (&thread_runner::run), const_cast<const thread_runner*> (this));
}
private:
std::unique_ptr<scl::thread> thread;
void run() const
{
std::println("PROCESSING...");
std::this_thread::sleep_for (1s);
std::println("...DONE");
}
};
int main() {
thread_runner tr;
}