Wrapper around std::thread with a built-in EventLoop. More...
#include <thread.h>


Public Types | |
| using | NativeHandle = std::thread::native_handle_type |
| Platform-specific native thread handle type. | |
Public Types inherited from ObjectBase | |
| using | SlotVariantFunc = std::function< void(const VariantList &)> |
| Function type for invoking a slot with a list of Variants. | |
Public Member Functions | |
| Thread (ObjectBase *parent=nullptr) | |
| Constructs a Thread (spawned mode). | |
| virtual | ~Thread () |
| Destructor. Waits for the thread if still running. | |
| void | start (size_t stackSize=0) |
| Starts the thread. | |
| Error | wait (unsigned int timeoutMs=0) |
| Waits for the thread to finish. | |
| EventLoop * | threadEventLoop () const |
| Returns the EventLoop associated with this thread. | |
| void | quit (int returnCode=0) |
| Requests the thread's event loop to quit. | |
| int | exitCode () const |
| Returns the exit code from the thread's event loop. | |
| bool | isRunning () const |
| Returns whether the thread is currently running. | |
| bool | isAdopted () const |
| Returns whether this is an adopted thread. | |
| bool | isCurrentThread () const |
| Returns whether the calling thread is this thread. | |
| uint64_t | nativeId () const |
| Returns the OS-native thread ID. | |
| SchedulePolicy | schedulePolicy () const |
| Returns the current scheduling policy of this thread. | |
| int | priority () const |
| Returns the current priority of this thread. | |
| Error | setPriority (int priority, SchedulePolicy policy=SchedulePolicy::Default) |
| Sets the scheduling policy and priority for this thread. | |
| String | name () const |
| Returns the name of this thread. | |
| void | setName (const String &name) |
| Sets the name of this thread. | |
| Set< int > | affinity () const |
| Returns the set of CPU cores this thread is allowed to run on. | |
| Error | setAffinity (const Set< int > &cpus) |
| Restricts this thread to run on the specified CPU cores. | |
| PROMEKI_SIGNAL (started) | |
| Emitted when the thread starts running. | |
| PROMEKI_SIGNAL (finished, int) | |
| Emitted when the thread finishes (carries the event loop exit code). | |
Public Member Functions inherited from ObjectBase | |
| ObjectBase (ObjectBase *p=nullptr) | |
| Default ObjectBase constructor. | |
| virtual | ~ObjectBase () |
| Destructor. Emits aboutToDestroy, detaches from parent, and destroys children. | |
| ObjectBase * | parent () const |
| Returns the parent object, if one. nullptr if none. | |
| void | setParent (ObjectBase *p) |
| Sets the parent of this object. If the object already has a parent, it will be removed as a child from the old parent and added as a child to the new one. | |
| const ObjectBaseList & | childList () const |
| Returns a list of children of this object. | |
| template<typename... Args> | |
| int | registerSlot (Slot< Args... > *slot) |
| Registers a slot with this object and assigns it an ID. | |
| EventLoop * | eventLoop () const |
| Returns the EventLoop this object is affiliated with. | |
| void | moveToThread (EventLoop *loop) |
| Changes the EventLoop affinity of this object. | |
| int | startTimer (unsigned int intervalMs, bool singleShot=false) |
| Starts a timer on this object's EventLoop. | |
| void | stopTimer (int timerId) |
| Stops a timer previously started with startTimer(). | |
| template<typename... Args> | |
| PROMEKI_NAMESPACE_BEGIN void | connect (Signal< Args... > *signal, Slot< Args... > *slot) |
Static Public Member Functions | |
| static Thread * | adoptCurrentThread () |
| Adopts the calling thread as a Thread object. | |
| static Thread * | currentThread () |
| Returns the Thread object for the calling thread. | |
| static uint64_t | currentNativeId () |
| Returns the OS-native thread ID of the calling thread. | |
| static int | priorityMin (SchedulePolicy policy=SchedulePolicy::Default) |
| Returns the minimum priority for a scheduling policy. | |
| static int | priorityMax (SchedulePolicy policy=SchedulePolicy::Default) |
| Returns the maximum priority for a scheduling policy. | |
| static unsigned int | idealThreadCount () |
| Returns the number of hardware threads available. | |
Static Public Member Functions inherited from ObjectBase | |
| static const MetaInfo & | metaInfo () |
| Returns the MetaInfo for the ObjectBase class. | |
| template<typename... Args> | |
| static void | connect (Signal< Args... > *signal, Slot< Args... > *slot) |
| connects a signal and slot together. This function assumes both the signal and slot exist in a ObjectBase or derived object | |
Protected Member Functions | |
| virtual void | run () |
| Override for custom thread behavior. | |
Protected Member Functions inherited from ObjectBase | |
| ObjectBase * | signalSender () |
| Returns the ObjectBase that emitted the signal currently being handled. | |
| virtual void | event (Event *e) |
| Called by EventLoop to deliver events to this object. | |
| virtual void | timerEvent (TimerEvent *e) |
| Called when a timer fires for this object. | |
Additional Inherited Members | |
Public Attributes inherited from ObjectBase | |
| Signal< ObjectBase * > | aboutToDestroySignal = Signal< ObjectBase * >(this, aboutToDestroySignalName) |
Static Public Attributes inherited from ObjectBase | |
| static constexpr const char * | aboutToDestroySignalName = PROMEKI_STRINGIFY( aboutToDestroy ) "(" PROMEKI_STRINGIFY_ARGS( ObjectBase * ) ")" |
| static SignalMeta | aboutToDestroySignalMeta = SignalMeta(metaInfo(), aboutToDestroySignalName) |
Wrapper around std::thread with a built-in EventLoop.
Derives from ObjectBase so it participates in parent/child tree and signal/slot. Operates in two modes:
| Thread::Thread | ( | ObjectBase * | parent = nullptr | ) |
Constructs a Thread (spawned mode).
| parent | Optional parent ObjectBase. |
Adopts the calling thread as a Thread object.
Used by Application for the main thread. The returned Thread is not owned by anyone — the caller manages its lifetime.
| Set< int > Thread::affinity | ( | ) | const |
Returns the set of CPU cores this thread is allowed to run on.
The thread must be running. Returns an empty set if the thread is not running, on error, or if the platform does not support CPU affinity queries.
Returns the OS-native thread ID of the calling thread.
On Linux this is the kernel TID (gettid()), on macOS the pthread thread ID (pthread_threadid_np()), and on Windows the Win32 thread ID (GetCurrentThreadId()).
|
inline |
Returns the exit code from the thread's event loop.
Only meaningful after the thread has finished.
Returns the number of hardware threads available.
Wraps std::thread::hardware_concurrency(). Returns 0 if the value cannot be determined.
|
inline |
Returns whether this is an adopted thread.
true for adopted threads, false for spawned.
|
inline |
Returns whether the calling thread is this thread.
true if the caller is running on this thread.
|
inline |
Returns whether the thread is currently running.
true if the thread is running. | String Thread::name | ( | ) | const |
Returns the name of this thread.
Returns the name previously set by setName(), or an empty string if no name has been set.
|
inline |
Returns the OS-native thread ID.
For spawned threads this is captured at thread start. For adopted threads it is captured at adoption time. Returns 0 if the thread has not yet started.
| int Thread::priority | ( | ) | const |
Returns the current priority of this thread.
The thread must be running. Returns 0 if the thread is not running or on error.
|
static |
Returns the maximum priority for a scheduling policy.
| policy | The scheduling policy to query. |
|
static |
Returns the minimum priority for a scheduling policy.
| policy | The scheduling policy to query. |
| Thread::PROMEKI_SIGNAL | ( | finished | , |
| int | |||
| ) |
Emitted when the thread finishes (carries the event loop exit code).
| void Thread::quit | ( | int | returnCode = 0 | ) |
Requests the thread's event loop to quit.
| returnCode | The exit code for the event loop. |
Override for custom thread behavior.
Default implementation runs the thread's EventLoop via exec().
| SchedulePolicy Thread::schedulePolicy | ( | ) | const |
Returns the current scheduling policy of this thread.
The thread must be running. Returns SchedulePolicy::Default if the thread is not running or on error.
Restricts this thread to run on the specified CPU cores.
The thread must be running. An empty set clears any affinity restriction, allowing the thread to run on all available cores.
| cpus | A set of zero-based CPU core indices. |
Sets the name of this thread.
The name is always stored locally. On supported platforms the OS-level thread name is also updated so that the name appears in debuggers and system tools (e.g. top -H, htop, ps -L).
If called before start(), the name is applied when the thread starts. The OS name is silently truncated to the platform limit (typically 15 characters on Linux/macOS).
On macOS the OS name can only be set from the thread itself, so cross-thread setName() updates the local name but not the OS name.
The logger's cached thread name is automatically updated when setName() is called from the thread itself.
| name | The name to assign. |
| Error Thread::setPriority | ( | int | priority, |
| SchedulePolicy | policy = SchedulePolicy::Default |
||
| ) |
Sets the scheduling policy and priority for this thread.
The thread must be running. Real-time policies (Fifo, RoundRobin) typically require elevated privileges.
| priority | The priority value. Use priorityMin() and priorityMax() to determine the valid range for the given policy. |
| policy | The scheduling policy to apply. |
| EventLoop * Thread::threadEventLoop | ( | ) | const |
Returns the EventLoop associated with this thread.
For spawned threads: valid after start(), before wait(). For adopted threads: returns the EventLoop currently active on that thread (looked up lazily, so it is valid as soon as an EventLoop is created on the adopted thread).
Waits for the thread to finish.
For spawned threads, blocks until the thread exits or the timeout expires. If the timeout expires the thread is still running and the caller may retry or call quit() first.
| timeoutMs | Maximum wait time in milliseconds. Zero (the default) waits indefinitely. |