Class thl::core::Thread#

class Thread#

A joinable OS thread with a requested scheduling class and a stop flag.

Composition, not inheritance: the body is a callable that polls should_stop() and returns. start() applies the priority and name on the new thread before running the body; the destructor requests a stop and joins. Not real-time safe except where noted.

thl::core::Thread worker;
worker.start({ThreadPriority::Low, "drain"}, [](const thl::core::Thread& self) {
    while (!self.should_stop()) { work(); }
});

Public Types

using Body = std::function<void(const Thread&)>#

Public Functions

Thread()#
~Thread()#

request_stop() + join().

Thread(const Thread&) = delete#
Thread &operator=(const Thread&) = delete#
Thread(Thread&&) = delete#
Thread &operator=(Thread&&) = delete#
bool start(const ThreadOptions &options, Body body)#

Start the thread.

Returns false (and does nothing) if it is already running or the OS refused to create a thread.

void request_stop() noexcept#

Ask the body to return. Real-time safe (one atomic store).

void join()#

Wait for the body to return. Safe to call when not running.

void detach()#

Give up ownership of the OS thread without waiting for it.

For the one case join() cannot serve: tearing the Thread down from its own body.

bool should_stop() const noexcept#

True after request_stop() (or ~Thread). Real-time safe.

bool is_running() const noexcept#

True from start() until the body has returned. Real-time safe.

bool joinable() const noexcept#

True while the OS thread exists (started and not yet joined).

Public Static Functions

static bool set_current_priority(ThreadPriority priority) noexcept#

Apply a scheduling class to the calling thread (one we did not create: a host’s audio thread, a benchmark’s main thread).

Returns whether the request was granted as asked.

static bool set_current_name(const char *name) noexcept#

Name the calling thread for debuggers and profilers.