Thread System 0.3.1
High-performance C++20 thread pool with work stealing and DAG scheduling
Loading...
Searching...
No Matches
thread_logger_init.cpp File Reference

Early initialization of thread_logger shutdown handler for SDOF prevention. More...

Include dependency graph for thread_logger_init.cpp:

Go to the source code of this file.

Detailed Description

Early initialization of thread_logger shutdown handler for SDOF prevention.

This file ensures that the thread_logger's atexit handler is registered as early as possible during program initialization, before any user-defined static objects are constructed. This prevents Static Destruction Order Fiasco (SDOF) by ensuring that is_shutting_down() returns true before any static object destructors run.

The Problem:

  • atexit handlers are called in LIFO order (reverse registration order)
  • If atexit(prepare_shutdown) is registered after static object A is created, then A's destructor runs AFTER prepare_shutdown(), not before
  • This means A's destructor may call thread_pool methods while is_shutting_down_ is still false, leading to SDOF issues

The Solution:

  • Register the atexit handler during early program initialization
  • On GCC/Clang: Use attribute((constructor)) with high priority
  • On MSVC: Use CRT initialization section pragma
  • This ensures the handler is registered before any user static objects, so prepare_shutdown() runs AFTER all user destructors complete

Related Issues:

  • GitHub issue #297: Improve atexit handler registration timing for SDOF prevention
  • Related: #295, #296 (initial SDOF prevention)
See also
thread_logger::prepare_shutdown()
thread_logger::is_shutting_down()

Definition in file thread_logger_init.cpp.