Examples › Thread Examples
threads_template
C++17 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_threads/template/testprog.cpp · Modern: examples_modern/threads/template/modern.cpp
Modern equivalent of the template example. The legacy program casts each argument to void * and back to TYPE * in the thread. std::thread is already a template: it takes the function and its arguments with their types, and copies them into the new thread, so no casts are needed and a wrong type is a compile error. A generic lambda (C++14) covers every type with one function.
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. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_threads/template/testprog.cpp 68 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 template <class TYPE> class SimpleThread : public gxThread { private: // Base class interface void *ThreadEntryRoutine(gxThread_t *thread); }; template <class TYPE> void *SimpleThread<TYPE>::ThreadEntryRoutine(gxThread_t *thread) // Thread's entry function { cout << "\n" << flush; TYPE info = *((TYPE *)thread->GetThreadParm()); cout << "Executing thread: " << info << "\n" << flush; return 0; } // Program's main thread of execution int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread<int> t1; SimpleThread<char> t2; SimpleThread<char *> t3; int i = 5; // Create and execute a thread gxThread_t *thread = t1.CreateThread((void *)&i); t1.sSleep(1); // Wait for the thread to exit delete thread; // Prevent memory leak char c = 'A'; thread = t2.CreateThread((void *)&c); t2.sSleep(1); delete thread; char *s = "TEST"; thread = t3.CreateThread((void *)&s); t3.sSleep(1); delete thread; return 0; // Exit the process, terminating all threads } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++17)
examples_modern/threads/template/modern.cpp 28 lines
#include <iostream> #include <string> #include <thread> template <class TYPE> void entry(TYPE info) { std::cout << "\nExecuting thread: " << info << "\n"; } int main() { std::thread(entry<int>, 5).join(); std::thread(entry<char>, 'A').join(); std::thread(entry<std::string>, "TEST").join(); // No char * casts // One generic lambda for every type auto print = [](const auto &info) { std::cout << "\nExecuting thread: " << info << "\n"; }; std::thread(print, 2.5).join(); std::thread(print, std::string("generic lambda")).join(); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=17 cmake --build build-modern --target threads_template ./build-modern/threads_template # or without CMake: g++ -std=c++17 -pthread -o threads_template examples_modern/threads/template/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Executing thread: 5 Executing thread: A Executing thread: TEST Executing thread: 2.5 Executing thread: generic lambda