DataReelDataReelC++ Transition Guide

Examples › Thread Examples

threads_simple

C++11 or laterC++ standard library onlyPortable C++ (tested on Linux)

Legacy: examples_threads/simple/testprog.cpp · Modern: examples_modern/threads/simple/modern.cpp

Modern equivalent of the simple gxThread example. A std::thread runs any callable (here a lambda), so no class has to be derived to get an entry routine. The exit routine becomes ordinary code at the end of the lambda, and main() joins the thread instead of sleeping and hoping the thread has finished.

Legacy calls in this program

LegacyModern C++StandardNotes
gxThread subclass, CreateThread()std::thread t(callable, args...)C++11Any function, lambda or functor runs in the thread; arguments are copied with their types (no void *).
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/simple/testprog.cpp 63 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

class SimpleThread : public gxThread
{
public:
  SimpleThread() { }
  ~SimpleThread() { }
  
private: // Base class interface
  void *ThreadEntryRoutine(gxThread_t *thread);
  void ThreadExitRoutine(gxThread_t *thread);
};


void *SimpleThread::ThreadEntryRoutine(gxThread_t *thread)
{
  cout << "\n" << flush;
  cout << "Executing thread..." << "\n" << flush;
  return 0;
}

void SimpleThread::ThreadExitRoutine(gxThread_t *thread)
{
  cout << "Exiting thread..." << "\n" << flush;
  cout << "\n" << flush;
}

int main()
{
#ifdef __MSVC_DEBUG__
  InitLeakTest();
#endif

  SimpleThread t;

  gxThread_t *tptr = t.CreateThread();

  t.sSleep(1); // Wait for the thread to exit

  delete tptr; // Prevent memory leaks
  
  return 0; // Exit the process, terminating all threads
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //


  

Modern C++ (C++11)

examples_modern/threads/simple/modern.cpp 21 lines
#include <iostream>
#include <thread>

int main()
{
  // gxThread: derive a class, override ThreadEntryRoutine() and
  // ThreadExitRoutine(), call CreateThread(), sSleep(1), delete the
  // gxThread_t pointer. std::thread: pass a callable.
  std::thread t([] {
    std::cout << "\nExecuting thread...\n";
    // What ThreadExitRoutine() did runs when the entry code is done
    std::cout << "Exiting thread...\n\n";
  });

  t.join(); // Wait for the thread; nothing to delete
  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_simple
./build-modern/threads_simple
# or without CMake:
g++ -std=c++11 -pthread -o threads_simple examples_modern/threads/simple/modern.cpp

Output of the modern program

From run_modern_tests.sh on RHEL 10, GCC 14, C++20.

Executing thread...
Exiting thread...