-
Notifications
You must be signed in to change notification settings - Fork 43
/
pthread_join_spawn_2.cpp
43 lines (35 loc) · 1.02 KB
/
pthread_join_spawn_2.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
/**
* @author github.com/luncliff (luncliff@gmail.com)
*/
#include <cstring>
#include <coroutine/pthread.h>
#include <coroutine/return.h>
using namespace std;
using namespace coro;
auto spawn_and_join(pthread_t& tid, const pthread_attr_t* attr)
-> pthread_joiner {
co_await attr;
tid = pthread_self();
co_return;
}
int main(int, char*[]) {
printf("before spawn: %llx \n", (uint64_t)pthread_self());
pthread_t tid{};
{
// joiner's destructor will wait for the new thread to return
auto joiner = spawn_and_join(tid, nullptr);
}
// the new thread's id must be visible at this moment
printf("after join: %llx \n", (uint64_t)tid);
if (tid == pthread_t{})
return __LINE__;
if (tid == pthread_self())
return __LINE__;
// it's already joined.
// so if we trying to join it again, the operation must fail
if (const auto ec = pthread_join(tid, nullptr); ec != ESRCH) {
puts(strerror(ec));
return __LINE__;
}
return EXIT_SUCCESS;
}