Examples › Thread Examples
threads_exiting
C++11 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_threads/exiting/testprog.cpp · Modern: examples_modern/threads/exiting/modern.cpp
Modern equivalent of the exiting example. A thread's result comes back through a std::future: std::async() runs the function and get() returns its value or rethrows its exception. gxThread::ExitThread(thread, code) and GetThreadExitCode() become "return code" and future.get(); an error can be an exception instead of a magic exit code.
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. |
| ExitThread(), GetThreadExitCode() | return a value; std::async / std::future::get() | C++11 | An exception thrown in the thread comes out of get(). |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_threads/exiting/testprog.cpp 84 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 // Constants const int THREADFAIL = 1; const int THREADPASS = 0; // Global variables int err_condition = 0; class SimpleThread : public gxThread { private: // Base class interface void *ThreadEntryRoutine(gxThread_t *thread); }; void *SimpleThread::ThreadEntryRoutine(gxThread_t *thread) { cout << "Executing thread" << "\n" << flush; if(err_condition) { cout << "Reached error condition!" << "\n" << flush; ExitThread(thread, THREADFAIL); } cout << "Exiting the thread with no errors reported" << "\n" << flush; return ExitThread(thread, THREADPASS); } void CheckExitCode(gxThread_t *thread) { cout << "Checking the thread's status" << "\n" << flush; if(int(thread->GetThreadExitCode()) == THREADPASS) cout << "The thread passed" << "\n" << flush; else if(int(thread->GetThreadExitCode()) == THREADFAIL) cout << "The thread failed" << "\n" << flush; else cout << "Invalid thread exit code reported" << "\n" << flush; cout << "\n" << flush; } int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread t; // Create and start a thread gxThread_t *thread = t.CreateThread(); // Wait for the thread to complete, and release its resources t.JoinThread(thread); CheckExitCode(thread); delete thread; // Simulate and error condition err_condition = 1; thread = t.CreateThread(); t.JoinThread(thread); CheckExitCode(thread); delete thread; return 0; // Exit the process } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++11)
examples_modern/threads/exiting/modern.cpp 50 lines
#include <future> #include <iostream> #include <stdexcept> enum class ThreadResult { Pass, Fail }; ThreadResult entry(bool err_condition) { std::cout << "Executing thread\n"; if(err_condition) { std::cout << "Reached error condition!\n"; return ThreadResult::Fail; // ExitThread(thread, THREADFAIL) } std::cout << "Exiting the thread with no errors reported\n"; return ThreadResult::Pass; } void check_exit_code(std::future<ThreadResult> &f) { std::cout << "Checking the thread's status\n"; try { ThreadResult r = f.get(); // Joins the thread std::cout << (r == ThreadResult::Pass ? "The thread passed\n" : "The thread failed\n"); } catch(const std::exception &e) { // An exception thrown in the thread std::cout << "The thread threw: " << e.what() << "\n"; } std::cout << "\n"; } int main() { auto f1 = std::async(std::launch::async, entry, false); check_exit_code(f1); auto f2 = std::async(std::launch::async, entry, true); // Error condition check_exit_code(f2); auto f3 = std::async(std::launch::async, []() -> ThreadResult { std::cout << "Executing thread\n"; throw std::runtime_error("error reported with an exception"); }); check_exit_code(f3); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=11 cmake --build build-modern --target threads_exiting ./build-modern/threads_exiting # or without CMake: g++ -std=c++11 -pthread -o threads_exiting examples_modern/threads/exiting/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Checking the thread's status Executing thread Exiting the thread with no errors reported The thread passed Checking the thread's status Executing thread Reached error condition! The thread failed Checking the thread's status Executing thread The thread threw: error reported with an exception