DataReelDataReelC++ Transition Guide

Examples › Thread Examples

threads_suspend

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

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

Modern equivalent of the suspend example. The standard library cannot suspend another thread (and SuspendThread() on a thread that holds a lock is a classic deadlock). A thread waits instead: here on a C++20 std::binary_semaphore that main() releases. In C++11 the same is done with a std::mutex, a std::condition_variable and a bool.

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 *).
DestroyThread(), delete gxThread_tnothing: the std::thread object owns the threadC++11Join or detach before the object is destroyed, or use std::jthread, which joins itself.
SuspendThread(), ResumeThread()std::binary_semaphore acquire()/release()C++20A thread waits for itself; no thread is stopped from outside (C++11: mutex + condition_variable + bool).
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.
gxThread_t IDs and state messagesstd::thread::id, std::this_thread::get_id(); program-owned std::atomic stateC++11

From the transition tables.

Side by side

DataReel 6.4 (legacy)

examples_threads/suspend/testprog.cpp 61 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"
#include "gxcond.h"
#include "gxmutex.h"

#ifdef __MSVC_DEBUG__
#include "leaktest.h"
#endif

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

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

// Main thread of execution
int main()
{
#ifdef __MSVC_DEBUG__
  InitLeakTest();
#endif

  SimpleThread t;

  gxThread_t *thread = t.CreateThread(); // Create and execute a thread
  cout << thread->ThreadStateMessage() << "\n" << flush;
  
  t.sSleep(1);
  cout << thread->ThreadStateMessage() << "\n" << flush;

  t.sSleep(1);
  if(t.ResumeThread(thread)) cout << "Error resuming thread" << "\n" << flush;
  cout << thread->ThreadStateMessage() << "\n" << flush;
  
  t.sSleep(1); // Wait for the thread to exit
  
  delete thread;
  return 0; // Exit the process
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //


  

Modern C++ (C++20)

examples_modern/threads/suspend/modern.cpp 32 lines
#include <atomic>
#include <chrono>
#include <iostream>
#include <semaphore>
#include <thread>

using namespace std::chrono_literals;

int main()
{
  std::binary_semaphore resume(0);
  std::atomic<bool> suspended{false};

  std::thread t([&] {
    suspended = true;
    resume.acquire(); // SuspendThread(thread): wait until released
    suspended = false;
    std::cout << "Executing thread...\n";
  });

  std::this_thread::sleep_for(100ms);
  std::cout << (suspended ? "Thread state: suspended\n"
                          : "Thread state: running\n");
  resume.release(); // ResumeThread(thread)
  t.join();
  std::cout << "Thread state: exited\n";
  return 0;
}
// ----------------------------------------------------------- // 
// ------------------------------- //
// --------- End of File --------- //
// ------------------------------- //

Build and run

cmake -S examples_modern -B build-modern -DCMAKE_CXX_STANDARD=20
cmake --build build-modern --target threads_suspend
./build-modern/threads_suspend
# or without CMake:
g++ -std=c++20 -pthread -o threads_suspend examples_modern/threads/suspend/modern.cpp

Output of the modern program

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

Thread state: suspended
Executing thread...
Thread state: exited