Examples › Thread Examples
threads_joined
C++11 or laterC++ standard library onlyPortable C++ (tested on Linux)
Legacy: examples_threads/joined/testprog.cpp · Modern: examples_modern/threads/joined/modern.cpp
Modern equivalent of the joined example. Each thread returns its exit code through a std::future (std::async), and future::get() is the join. The legacy shared counter "thread_count++" is a data race; here it is a std::atomic<int>.
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. |
From the transition tables.
Side by side
DataReel 6.4 (legacy)
examples_threads/joined/testprog.cpp 76 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 // Global variables int thread_count = 0; class SimpleThread : public gxThread { public: SimpleThread() { } ~SimpleThread() { } private: // Base class interface void *ThreadEntryRoutine(gxThread_t *thread); }; void *SimpleThread::ThreadEntryRoutine(gxThread_t *thread) { cout << "Excuting thread: " << thread_count++ << "\n" << flush; return (void *)THREAD_EXIT_CODE; } // 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(); t.sSleep(1); } // Wait for all the threads to finish before exiting for(i = 0; i < num_threads; ++i) { if(t.JoinThread(thread[i]) != 0) cout << "Could not join the thread" << "\n"; if(thread[i]->GetThreadExitCode() != THREAD_EXIT_CODE) cout << "This thread exited with an error" << "\n"; } // Prevent memory leaks for(i = 0; i < num_threads; ++i) delete thread[i]; return 0; // Exit the process } // ----------------------------------------------------------- // // ------------------------------- // // --------- End of File --------- // // ------------------------------- //
Modern C++ (C++11)
examples_modern/threads/joined/modern.cpp 31 lines
#include <atomic> #include <future> #include <iostream> #include <vector> const int THREAD_EXIT_CODE = 15; int main() { std::atomic<int> thread_count{0}; const int num_threads = 10; std::vector<std::future<int>> results; for(int i = 0; i < num_threads; i++) { results.push_back(std::async(std::launch::async, [&thread_count] { std::cout << "Executing thread: " << thread_count++ << "\n"; return THREAD_EXIT_CODE; })); results.back().wait(); // Keep the output in order, like sSleep(1) } for(auto &r : results) { if(r.get() != THREAD_EXIT_CODE) std::cout << "This thread exited with an error\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_joined ./build-modern/threads_joined # or without CMake: g++ -std=c++11 -pthread -o threads_joined examples_modern/threads/joined/modern.cpp
Output of the modern program
From run_modern_tests.sh on RHEL 10, GCC 14, C++20.
Executing thread: 0 Executing thread: 1 Executing thread: 2 Executing thread: 3 Executing thread: 4 Executing thread: 5 Executing thread: 6 Executing thread: 7 Executing thread: 8 Executing thread: 9