Examples › Thread Examples
threads_timed
C++14 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_threads/timed/testprog.cpp · Modern: examples_modern/threads/timed/modern.cpp
Modern equivalent of the timed example. std::this_thread::sleep_for() with std::chrono durations replaces sSleep(), mSleep(), uSleep() and nSleep(): one function, and the unit is part of the type, so 1s, 1000ms and 1000000us are the same duration (the literals are C++14; in C++11 write std::chrono::seconds(1)). The thread's message is passed to the thread as an argument instead of a public char * member.
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/timed/testprog.cpp 88 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 class SimpleThread : public gxThread { public: SimpleThread() { } ~SimpleThread() { } public: char *message; private: // Base class interface void *ThreadEntryRoutine(gxThread_t *thread); }; void *SimpleThread::ThreadEntryRoutine(gxThread_t *thread) { cout << "\n" << flush; cout << message << "\n" << flush; return 0; } int main() { #ifdef __MSVC_DEBUG__ InitLeakTest(); #endif SimpleThread t; char *message1 = "Test message 1"; char *message2 = "Test message 2"; t.message = message1; gxThread_t *thread = t.CreateThread(); t.sSleep(1); // Wait for the first thread to exit delete thread; // Prevent memory leak t.message = message2; thread = t.CreateThread(); t.sSleep(1); // Wait for the second thread to exit delete thread; cout << "\n" << flush; cout << "Testing all the reentrant gxThread sleep functions..." << "\n" << flush; cout << "Sleeping for 1 second" << "\n" << flush; t.sSleep(1); cout << "Sleeping for 1000 milliseconds" << "\n" << flush; t.mSleep(1000); cout << "Sleeping for 1000000 microseconds" << "\n" << flush; t.uSleep(1000000); cout << "Sleeping for 100 microseconds" << "\n" << flush; t.uSleep(100); cout << "Sleeping for 1000000000 nanoseconds" << "\n" << flush; t.nSleep(1000000000); cout << "Sleeping for 100 nanoseconds" << "\n" << flush; t.nSleep(100); return 0; // Exit the process } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++14)
examples_modern/threads/timed/modern.cpp 35 lines
#include <chrono> #include <iostream> #include <string> #include <thread> using namespace std::chrono_literals; int main() { auto entry = [](const std::string &message) { std::cout << "\n" << message << "\n"; }; std::thread(entry, "Test message 1").join(); std::thread(entry, "Test message 2").join(); std::cout << "\nTesting the std::chrono sleep durations...\n"; auto timed = [](const char *what, auto d) { auto start = std::chrono::steady_clock::now(); std::this_thread::sleep_for(d); auto took = std::chrono::duration_cast<std::chrono::microseconds>( std::chrono::steady_clock::now() - start); std::cout << "Slept for " << what << " (" << took.count() << " us)\n"; }; timed("1s", 1s); timed("1000ms", 1000ms); timed("1000000us", 1000000us); timed("100us", 100us); timed("1000000000ns", 1000000000ns); timed("100ns", 100ns); return 0; } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Build and run
cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=14 cmake --build build-modern --target threads_timed ./build-modern/threads_timed # or without CMake: g++ -std=c++14 -pthread -o threads_timed examples_modern/threads/timed/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Test message 1 Test message 2 Testing the std::chrono sleep durations... Slept for 1s (1000071 us) Slept for 1000ms (1000065 us) Slept for 1000000us (1000067 us) Slept for 100us (155 us) Slept for 1000000000ns (1000071 us) Slept for 100ns (1506 us)