Thread class
#include <nCine/Threading/Thread.h>
Operating system thread.
Owns and runs a thread of execution. A thread can be created from a plain ThreadFuncDelegate with a user argument or from any callable and its arguments, and is started immediately on construction. Provides joining and detaching, naming, priority and affinity control and various static helpers for the calling thread. Most functionality requires WITH_THREADS; otherwise only Sleep() is available.
Public types
- using ThreadFuncDelegate = void(*)(void*)
- Plain function pointer used as a thread entry point.
Public static functions
-
static void Sleep(std::
uint32_t milliseconds) - Puts the calling thread to sleep for the given number of milliseconds.
-
static auto GetProcessorCount() -> std::
uint32_t - Returns the number of processors in the machine.
- static void SetCurrentName(const char* name)
- Sets the calling thread name (not supported on Emscripten and Switch).
-
static auto GetCurrentId() -> std::
uintptr_t - Returns the calling thread ID.
- static void Exit()
- Terminates the calling thread.
- static void YieldExecution()
- Yields the calling thread in favour of another one with the same priority.
Constructors, destructors, conversion operators
- Thread()
- Default constructor.
- Thread(ThreadFuncDelegate threadFunc, void* threadArg) explicit
- Creates a thread around a function and runs it immediately.
-
template<class Fn, class ... Args, std::Thread(Fn&& fn, Args && ... args) explicit
enable_if_t<!std:: is_same<std:: remove_cv_t<std:: remove_reference_t<Fn>>, Thread>::value && !std:: is_convertible<std:: decay_t<Fn>, ThreadFuncDelegate>::value, int> = 0> - ~Thread()
- Thread(const Thread& other)
- Thread(Thread&& other) noexcept
- operator bool() const explicit
- Whether the thread is running.
Public functions
- auto operator=(const Thread& other) -> Thread&
- auto operator=(Thread&& other) -> Thread& noexcept
- auto Join() -> bool
- Joins the thread.
- void Detach()
- Detaches the running thread from the object.
- void SetName(const char* name)
- Sets the thread name (not supported on Apple, Emscripten and Switch).
-
auto GetPriority() const -> std::
int32_t - Gets the thread priority.
-
void SetPriority(std::
int32_t priority) - Sets the thread priority.
- auto Abort() -> bool
- Tries to cancel or terminate the thread (depending on operating system).
- auto GetAffinityMask() const -> ThreadAffinityMask
- Gets the thread affinity mask.
- void SetAffinityMask(ThreadAffinityMask affinityMask)
- Sets the thread affinity mask.
Function documentation
template<class Fn, class ... Args, std:: enable_if_t<!std:: is_same<std:: remove_cv_t<std:: remove_reference_t<Fn>>, Thread>::value && !std:: is_convertible<std:: decay_t<Fn>, ThreadFuncDelegate>::value, int> = 0>
nCine:: Thread:: Thread(Fn&& fn,
Args && ... args) explicit
This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.