ezEngine Release 26.3
Loading...
Searching...
No Matches
ezThreadUtils Class Reference

Contains general thread functions. More...

#include <ThreadUtils.h>

Static Public Member Functions

static void YieldTimeSlice ()
 Suspends execution of the current thread and yields the remaining time slice to other threads.
 
static void YieldHardwareThread ()
 Yields execution to other hardware threads on the same physical processor core.
 
static void Sleep (const ezTime &duration)
 Suspends the execution of the current thread for the given amount of time.
 
static bool IsMainThread ()
 Checks if the current thread is the main thread.
 
static ezThreadID GetCurrentThreadID ()
 Returns a unique identifier for the currently executing thread.
 

Detailed Description

Contains general thread functions.

Member Function Documentation

◆ GetCurrentThreadID()

ezThreadID ezThreadUtils::GetCurrentThreadID ( )
static

Returns a unique identifier for the currently executing thread.

The returned ID is guaranteed to be unique among all currently running threads, but may be reused after a thread terminates. Thread IDs should not be stored long-term or used for cross-process communication. Primarily useful for debugging, logging, and temporary thread identification.

◆ IsMainThread()

bool ezThreadUtils::IsMainThread ( )
static

Checks if the current thread is the main thread.

The main thread is defined as the thread that initialized the Foundation library. This is useful for assertions and ensuring certain operations only happen on the main thread (e.g., UI operations, single-threaded subsystem access). Returns true only for the thread that called Foundation startup.

◆ Sleep()

void ezThreadUtils::Sleep ( const ezTime duration)
static

Suspends the execution of the current thread for the given amount of time.

The actual sleep duration may be longer than requested due to OS scheduling granularity and system load. Precision varies by platform but is typically around 1-15ms. For high-precision timing, consider using busy-wait loops with YieldTimeSlice() for very short delays, though this consumes more CPU. Avoid using Sleep() in performance-critical code paths.

◆ YieldHardwareThread()

void ezThreadUtils::YieldHardwareThread ( )
static

Yields execution to other hardware threads on the same physical processor core.

This is a hint to the processor to allow other hardware threads (hyperthreads) on the same core to execute. Only useful on processors with simultaneous multithreading (SMT/hyperthreading). Does nothing on processors without hardware threading support. Use this in tight loops where you're waiting for memory operations or when you want to be more cooperative with hardware threads on the same core without giving up the time slice to other processes.

◆ YieldTimeSlice()

void ezThreadUtils::YieldTimeSlice ( )
static

Suspends execution of the current thread and yields the remaining time slice to other threads.

This allows other threads or processes to run. Use this in spin-wait loops or when the current thread is waiting for work from other threads. On most platforms this translates to a scheduler yield. Prefer this over YieldHardwareThread() for general cooperative multitasking scenarios.


The documentation for this class was generated from the following files: