// 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; }