From 82e9ac4163b46b59e121194f84ac370818482923 Mon Sep 17 00:00:00 2001 From: johannst Date: Thu, 15 Jul 2021 21:20:14 +0200 Subject: use proper date fmt in content file names that zola can automatically can derive the date --- .../2021-05-15-pthread_cancel-noexcept/thread.cc | 40 ++++++++++++++++++++++ 1 file changed, 40 insertions(+) create mode 100644 content/2021-05-15-pthread_cancel-noexcept/thread.cc (limited to 'content/2021-05-15-pthread_cancel-noexcept') diff --git a/content/2021-05-15-pthread_cancel-noexcept/thread.cc b/content/2021-05-15-pthread_cancel-noexcept/thread.cc new file mode 100644 index 0000000..73370be --- /dev/null +++ b/content/2021-05-15-pthread_cancel-noexcept/thread.cc @@ -0,0 +1,40 @@ +// file : thread.cc +// compile: g++ thread.cc -o thread -lpthread + +#include + +#include +#include + +struct S { + ~S() { + const char msg[] = "cancellation-point\n"; + // write() -> pthread cancellation point. + write(STDOUT_FILENO, msg, sizeof(msg)); + } +}; + +std::atomic 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; +} -- cgit v1.2.3