Examples › Thread Examples
threads_message
C++11 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_threads/message/testprog.cpp · Modern: examples_modern/threads/message/modern.cpp
Modern equivalent of the message example. The standard library keeps no per-thread status messages; what it gives is std::thread::id (std::this_thread::get_id()), native_handle() for the platform API and std::thread::hardware_concurrency(). The object and class IDs and the exit code that gxThread_t carried are ordinary members of a struct the program owns, and the thread returns its exit code through a future.
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(). |
| 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. |
| SetThreadPriority() | t.native_handle() + pthread_setschedparam() / SetThreadPriority() | platform | The standard has no priorities; a std::priority_queue orders work. |
| CreateThread(..., stack size) | pthread_attr_setstacksize() / _beginthreadex() | platform | std::thread cannot set a stack size. |
| 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/message/testprog.cpp 109 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 <stdlib.h> #include "gxthread.h" #ifdef __MSVC_DEBUG__ #include "leaktest.h" #endif // Constants const int THREAD_EXIT_CODE = 15; // Arbitrary exit code value void PrintThreadStatus(gxThread_t *thread) { cout << "Thread OID: " << (int)thread->GetObjectID() << "\n" << flush; cout << "Thread CID: " << (int)thread->GetClassID() << "\n" << flush; cout << thread->ThreadExceptionMessage() << "\n" << flush; cout << thread->ThreadPriorityMessage() << "\n" << flush; cout << thread->ThreadStateMessage() << "\n" << flush; cout << thread->ThreadTypeMessage() << "\n" << flush; if(thread->GetStackSize() > (gxStackSizeType)0) cout << "Stack size: " << (int)thread->GetStackSize() << "\n" << flush; if(thread->GetThreadExitCode() == (gxThreadExitCode)THREAD_EXIT_CODE) cout << "Exit code: " << (int)thread->GetThreadExitCode() << "\n" << flush; } class SimpleThread : public gxThread { public: SimpleThread() { cid = 1500; oid = 0; } ~SimpleThread() { } private: // Base class interface void *ThreadEntryRoutine(gxThread_t *thread); void ThreadExitRoutine(gxThread_t *thread); private: gxThreadObjectID oid; gxThreadClassID cid; }; void *SimpleThread::ThreadEntryRoutine(gxThread_t *thread) { thread->SetClassID(cid); thread->SetObjectID(oid++); PrintThreadStatus(thread); sSleep(1); return (void *)THREAD_EXIT_CODE; } void SimpleThread::ThreadExitRoutine(gxThread_t *thread) { cout << "\n" << flush; PrintThreadStatus(thread); cout << "\n" << flush; } // Main thread of execution int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread t; const int NUM_THREADS = 10; gxThread_t *thread[NUM_THREADS]; int i; for(i = 0; i < NUM_THREADS; i++) { thread[i] = t.CreateThread(); // Check for any errors created during thread creation if(thread[i]->GetThreadError() != 0) { cout << thread[i]->ThreadExceptionMessage() << "\n" << flush; } t.sSleep(3); } // Wait for all the threads to finish before exiting for(i = 0; i < NUM_THREADS; i++) t.JoinThread(thread[i]); // Cleanup all the gxThread_t pointers for(i = 0; i < NUM_THREADS; i++) { int rv = t.DestroyThread(thread[i]); if(rv != 0) { cout << "Error destroying thread!" << "\n" << flush; } } return 0; // Exit the process } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++11)
examples_modern/threads/message/modern.cpp 55 lines
#include <future> #include <iostream> #include <mutex> #include <sstream> #include <thread> #include <vector> const int THREAD_EXIT_CODE = 15; struct ThreadInfo { // What gxThread_t carried int object_id = 0; // SetObjectID() int class_id = 1500; // SetClassID() std::thread::id tid; // The standard thread ID }; std::mutex out; void print_status(const ThreadInfo &info, const char *where) { std::ostringstream id; id << info.tid; std::lock_guard<std::mutex> lock(out); std::cout << where << ": OID " << info.object_id << ", CID " << info.class_id << ", std::thread::id " << id.str() << "\n"; } int main() { const int NUM_THREADS = 10; std::cout << "Hardware threads: " << std::thread::hardware_concurrency() << "\n"; std::vector<std::future<int>> results; for(int i = 0; i < NUM_THREADS; i++) { results.push_back(std::async(std::launch::async, [i] { ThreadInfo info; info.object_id = i; info.tid = std::this_thread::get_id(); print_status(info, "Entry"); print_status(info, "Exit "); return THREAD_EXIT_CODE; })); } for(auto &r : results) { int code = r.get(); if(code != THREAD_EXIT_CODE) std::cout << "Bad exit code " << code << "\n"; } std::cout << "All " << NUM_THREADS << " threads returned exit code " << THREAD_EXIT_CODE << "\n"; 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_message ./build-modern/threads_message # or without CMake: g++ -std=c++11 -pthread -o threads_message examples_modern/threads/message/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Hardware threads: 8 Entry: OID 0, CID 1500, std::thread::id 140590072690368 Exit : OID 0, CID 1500, std::thread::id 140590072690368 Entry: OID 1, CID 1500, std::thread::id 140590064297664 Exit : OID 1, CID 1500, std::thread::id 140590064297664 Entry: OID 2, CID 1500, std::thread::id 140590055904960 Exit : OID 2, CID 1500, std::thread::id 140590055904960 Entry: OID 3, CID 1500, std::thread::id 140590047512256 Exit : OID 3, CID 1500, std::thread::id 140590047512256 Entry: OID 4, CID 1500, std::thread::id 140590039119552 Exit : OID 4, CID 1500, std::thread::id 140590039119552 Entry: OID 5, CID 1500, std::thread::id 140590030726848 Exit : OID 5, CID 1500, std::thread::id 140590030726848 Entry: OID 6, CID 1500, std::thread::id 140590022334144 Exit : OID 6, CID 1500, std::thread::id 140590022334144 Entry: OID 7, CID 1500, std::thread::id 140590013941440 Exit : OID 7, CID 1500, std::thread::id 140590013941440 Entry: OID 8, CID 1500, std::thread::id 140590005548736 Exit : OID 8, CID 1500, std::thread::id 140590005548736 Entry: OID 9, CID 1500, std::thread::id 140589997156032 Exit : OID 9, CID 1500, std::thread::id 140589997156032 All 10 threads returned exit code 15