-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstd_thread.cpp
executable file
·99 lines (76 loc) · 1.92 KB
/
std_thread.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <iostream>
using std::cout;
struct place_holder
{
virtual ~place_holder() = default;
virtual void invoke() = 0;
};
template<typename Func>
struct holder : place_holder
{
Func fn_;
holder(Func&& fn)
: fn_(std::forward<Func>(fn))
{}
void invoke() override
{
cout << "will invoke function\n";
// might be better to use std::invoke here
fn_();
}
};
void* pthread_func(void* ctx)
{
cout << "pthread started\n";
place_holder* ph = static_cast<place_holder*>(ctx);
ph->invoke();
delete ph;
return nullptr;
}
// simple thread class that provides for an argless and void returning function
// to be passed to the new thread. It's obviously way more complex for the
// various other combinations of variadic args and member function pointers
// (detected and dispatched using sfinae).
class thread
{
pthread_t handle_;
static void* thread_fn(void* ctx)
{
place_holder* ph = static_cast<place_holder*>(ctx);
ph->invoke();
free(ph);
return nullptr;
}
public:
template<typename Func>
thread(Func&& fn)
{
pthread_create(&handle_,
nullptr,
thread_fn,
new holder(std::forward<Func>(fn)));
}
thread(const thread&) = delete;
thread& operator=(const thread&) = delete;
void join()
{
pthread_join(handle_, nullptr);
}
};
int main()
{
/*
* use holder directly with pthread api.
*/
pthread_t handle;
// Its not possible to name the type here (at least not
// portably as the type signature involves a lambda).
auto h = new holder([](){ cout << "hi from thread\n"; });
pthread_create(&handle, nullptr, &pthread_func, h);
pthread_join(handle, nullptr);
/*
* use thread object
*/
thread t1([](){ cout << "hello again\n"; });
t1.join();
}