blob: 73370be50c873cbb7906bfc11ed2100e51d86481 (
plain) (
blame)
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
|
// file : thread.cc
// compile: g++ thread.cc -o thread -lpthread
#include <atomic>
#include <pthread.h>
#include <unistd.h>
struct S {
~S() {
const char msg[] = "cancellation-point\n";
// write() -> pthread cancellation point.
write(STDOUT_FILENO, msg, sizeof(msg));
}
};
std::atomic<bool> gReleaseThread{false};
void* threadFn(void*) {
while (!gReleaseThread) {}
// Hit cancellation point in destructor which
// is implicitly `noexcept`.
S s;
return nullptr;
}
int main() {
pthread_t t;
pthread_create(&t, nullptr /* attr */, threadFn, nullptr /* arg */);
// Cancel thread and release it to hit the cancellation point.
pthread_cancel(t);
gReleaseThread = true;
pthread_join(t, nullptr /* retval */);
return 0;
}
|