34 using std::atomic_uint;
35 using std::chrono::steady_clock;
36 using std::chrono_literals::operator
""ms;
44 const auto SHUTDOWN_GRACE_PERIOD = 20ms;
47 lifecycleMsg (
Literal phase,
string threadID)
49 return _Fmt{
"Thread '%s' %s"} % threadID % phase;
58 static atomic_uint globalCnt{1};
59 return _Fmt{
"%s.%03i"} % rawID % globalCnt.fetch_add (+1, std::memory_order_acq_rel);
67 return threadImpl_.get_id() == std::this_thread::get_id();
72 ThreadWrapper::markThreadStart()
74 TRACE (thread,
"%s", lifecycleMsg (
"start...", threadID_).c_str());
80 ThreadWrapper::markThreadEnd()
82 TRACE (thread,
"%s", lifecycleMsg (
"terminates.", threadID_).c_str());
87 ThreadWrapper::setThreadName()
89 pthread_t nativeHandle = threadImpl_.native_handle();
92 pthread_setname_np(nativeHandle, threadID_.substr(0, 15).c_str());
97 ThreadWrapper::waitGracePeriod() noexcept
100 auto start = steady_clock::now();
101 while (threadImpl_.joinable()
102 and steady_clock::now () - start < SHUTDOWN_GRACE_PERIOD
104 std::this_thread::yield();
108 if (threadImpl_.joinable())
109 ALERT (thread,
"Thread '%s' failed to terminate after grace period. Abort.", threadID_.c_str());
bool invokedWithinThread() const
detect if the currently executing code runs within this thread
#define ERROR_LOG_AND_IGNORE(_FLAG_, _OP_DESCR_)
convenience shortcut for a sequence of catch blocks just logging and consuming an error...
inline string literal This is a marker type to indicate that
A front-end for using printf-style formatting.
Implementation namespace for support and library code.
Marker types to indicate a literal string and a Symbol.
Tiny helper functions and shortcuts to be used everywhere Consider this header to be effectively incl...
Convenience front-end to simplify and codify basic thread handling.
static string decorate_with_global_count(string const &)
Helper to create a suffix to the thread-ID with running count.