Examples › Thread Examples
threads_cancel
C++20 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_threads/cancel/testprog.cpp · Modern: examples_modern/threads/cancel/modern.cpp
Modern equivalent of the cancel example. gxThread::CancelThread() uses pthread_cancel() (asynchronous cancellation) on POSIX. The standard library has no forced cancellation: C++20 std::jthread gives each thread a std::stop_token, and the thread checks it and returns. The cleanup handler becomes a destructor (RAII), which runs on every way out of the function. std::jthread also joins in its destructor.
Legacy calls in this program
| Legacy | Modern C++ | Standard | Notes |
|---|---|---|---|
| gxThread subclass, CreateThread() | std::thread t(callable, args...) | C++11 | Any function, lambda or functor runs in the thread; arguments are copied with their types (no void *). |
| JoinThread() | t.join(); std::future::get() | C++11 | join() waits; a std::async future's get() also returns the result. |
| DestroyThread(), delete gxThread_t | nothing: the std::thread object owns the thread | C++11 | Join or detach before the object is destroyed, or use std::jthread, which joins itself. |
| CancelThread(), ThreadCleanupHandler() | std::jthread::request_stop() + std::stop_token; cleanup in a destructor | C++20 | Cancellation is cooperative: the thread checks stop_requested() or waits on a condition_variable_any with the token. |
| sSleep(), mSleep(), uSleep(), nSleep() | std::this_thread::sleep_for(1s), (500ms), (100us), (100ns) | C++11 (literals C++14) | The unit is part of the std::chrono duration type. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_threads/cancel/testprog.cpp 74 lines
#include "gxdlcode.h" #if defined (__USE_ANSI_CPP__) // Use the ANSI Standard C++ library #include <iostream> using namespace std; // Use unqualified names for Standard C++ library #else // Use the old iostream library by default #include <iostream.h> #endif // __USE_ANSI_CPP__ #include "gxthread.h" #ifdef __MSVC_DEBUG__ #include "leaktest.h" #endif class SimpleThread : public gxThread { public: SimpleThread() { } ~SimpleThread() { } private: // Base class interface void *ThreadEntryRoutine(gxThread_t *thread); void ThreadCleanupHandler(gxThread_t *thread); }; void *SimpleThread::ThreadEntryRoutine(gxThread_t *thread) { cout << "\n" << flush; cout << "Executing thread..." << "\n" << flush; while (1) { cout << "Thread: Looping through a long running request" << "\n" << flush; sSleep(1); } return 0; } void SimpleThread::ThreadCleanupHandler(gxThread_t *thread) { cout << "Executing clean up routine..." << "\n" << flush; cout << "\n" << flush; } int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread t; gxThread_t *thread = t.CreateThread(); // Wait until we realize the thread needs to be canceled t.sSleep(3); t.CancelThread(thread); // Wait for the thread to complete, and release its resources t.JoinThread(thread); if(thread->GetThreadState() == gxTHREAD_STATE_CANCELED) { cout << "The thread was canceled" << "\n" << flush; } else { cout << "The thread was not canceled" << "\n" << flush; } delete thread; // Prevent memory leaks return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++20)
examples_modern/threads/cancel/modern.cpp 43 lines
#include <chrono> #include <condition_variable> #include <iostream> #include <mutex> #include <stop_token> #include <thread> using namespace std::chrono_literals; struct Cleanup { // Replaces ThreadCleanupHandler() ~Cleanup() { std::cout << "Executing clean up routine...\n\n"; } }; int main() { bool canceled = false; std::jthread t([&canceled](std::stop_token stop) { Cleanup cleanup; std::mutex m; std::condition_variable_any cv; std::cout << "\nExecuting thread...\n"; while(!stop.stop_requested()) { std::cout << "Thread: Looping through a long running request\n"; // Sleep for 1 second, but wake up at once when a stop is requested std::unique_lock<std::mutex> lock(m); cv.wait_for(lock, stop, 1s, [] { return false; }); } canceled = true; }); std::this_thread::sleep_for(3s); // Wait until the thread must stop t.request_stop(); // gxThread::CancelThread() t.join(); // gxThread::JoinThread() std::cout << (canceled ? "The thread was canceled\n" : "The thread was not canceled\n"); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=20 cmake --build build-modern --target threads_cancel ./build-modern/threads_cancel # or without CMake: g++ -std=c++20 -pthread -o threads_cancel examples_modern/threads/cancel/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Executing thread... Thread: Looping through a long running request Thread: Looping through a long running request Thread: Looping through a long running request Executing clean up routine... The thread was canceled