43 using std::atomic_uint;
44 using std::chrono::steady_clock;
45 using std::chrono_literals::operator
""ms;
53 const auto SHUTDOWN_GRACE_PERIOD = 20ms;
56 lifecycleMsg (
Literal phase,
string threadID)
58 return _Fmt{
"Thread '%s' %s"} % threadID % phase;
67 static atomic_uint globalCnt{1};
68 return _Fmt{
"%s.%03i"} % rawID % globalCnt.fetch_add (+1, std::memory_order_acq_rel);
76 return threadImpl_.get_id() == std::this_thread::get_id();
81 ThreadWrapper::markThreadStart()
83 TRACE (thread,
"%s", lifecycleMsg (
"start...", threadID_).c_str());
89 ThreadWrapper::markThreadEnd()
91 TRACE (thread,
"%s", lifecycleMsg (
"terminates.", threadID_).c_str());
96 ThreadWrapper::setThreadName()
98 pthread_t nativeHandle = threadImpl_.native_handle();
101 pthread_setname_np(nativeHandle, threadID_.substr(0, 15).c_str());
106 ThreadWrapper::waitGracePeriod() noexcept
109 auto start = steady_clock::now();
110 while (threadImpl_.joinable()
111 and steady_clock::now () - start < SHUTDOWN_GRACE_PERIOD
113 std::this_thread::yield();
117 if (threadImpl_.joinable())
118 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.