Examples › Thread Examples
threads_destroy
C++11 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_threads/destroy/testprog.cpp · Modern: examples_modern/threads/destroy/modern.cpp
Modern equivalent of the destroy example. A std::thread object owns its thread: there is no gxThread_t to delete and no DestroyThread(). The rule is that a std::thread must be joined or detached before it is destroyed (std::terminate() is called otherwise). The thread state that gxThread_t kept (ThreadStateMessage()) is kept here by the program in a std::atomic, which is safe to read from another thread.
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 *). |
| 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. |
| 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. |
| gxThread_t IDs and state messages | std::thread::id, std::this_thread::get_id(); program-owned std::atomic state | C++11 |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_threads/destroy/testprog.cpp 58 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 { private: // Base class interface void *ThreadEntryRoutine(gxThread_t *thread); }; void *SimpleThread::ThreadEntryRoutine(gxThread_t *thread) // Thread's entry function { cout << "Executing thread..." << "\n" << flush; return 0; } int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread t; cout << "Creating thread..." << "\n" << flush; gxThread_t *thread = t.CreateThread(); cout << thread->ThreadStateMessage() << "\n" << flush; t.sSleep(1); cout << thread->ThreadStateMessage() << "\n" << flush; t.sSleep(1); cout << "Destroying the thread..." << "\n" << flush; t.DestroyThread(thread); return 0; // Exit the process } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++11)
examples_modern/threads/destroy/modern.cpp 38 lines
#include <atomic> #include <chrono> #include <iostream> #include <thread> enum class State { New, Running, Exited }; const char *to_string(State s) { switch(s) { case State::New: return "Thread state: new"; case State::Running: return "Thread state: running"; case State::Exited: return "Thread state: exited"; } return "Thread state: unknown"; } int main() { std::atomic<State> state{State::New}; std::cout << "Creating thread...\n"; std::thread t([&state] { state = State::Running; std::cout << "Executing thread...\n"; state = State::Exited; }); std::cout << to_string(state) << "\n"; t.join(); // Instead of sSleep(1) and DestroyThread() std::cout << to_string(state) << "\n"; std::cout << "Destroying the thread object...\n"; return 0; // ~thread() is safe: the thread was joined } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=11 cmake --build build-modern --target threads_destroy ./build-modern/threads_destroy # or without CMake: g++ -std=c++11 -pthread -o threads_destroy examples_modern/threads/destroy/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Creating thread... Thread state: new Executing thread... Thread state: exited Destroying the thread object...