CX Framework
Cross-platform C utility framework
Loading...
Searching...
No Matches
Core Multithreading

Macros

#define thrRun(func, name, ...)    _thrRun(func, name, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ })
 
#define thrSetPriority(thread, prio)   _thrPlatformSetPriority(thread, THREAD_##prio)
 
#define thrSetPriorityV(thread, prio)   _thrPlatformSetPriority(thread, prio)
 
#define thrRelease(pthread)   objRelease(pthread)
 

Enumerations

enum  ThreadPriority {
  THREAD_Normal = 0 , THREAD_Batch , THREAD_Low , THREAD_Idle ,
  THREAD_High , THREAD_Higher , THREAD_Realtime
}
 Thread scheduling priority levels. More...
 

Functions

Thread * thrCurrent (void)
 
intptr thrOSThreadID (Thread *thread)
 
intptr thrCurrentOSThreadID (void)
 
bool thrRunning (Thread *thread)
 
bool thrLoop (Thread *thread)
 
bool thrRequestExit (Thread *thread)
 
bool thrWait (Thread *thread, int64 timeout)
 
bool thrShutdown (Thread *thread)
 
int thrShutdownMany (sa_Thread threads)
 
void thrRegisterSysThread (Thread *thread)
 

Detailed Description

Macro Definition Documentation

◆ thrRelease

#define thrRelease (   pthread)    objRelease(pthread)

void thrRelease(Thread **pthread)

Release a reference to a thread object.

Decrements the reference count and destroys the thread if it reaches zero. The thread continues executing even after releasing the reference, but memory will be leaked if you don't release all references.

Parameters
pthreadPointer to thread object pointer (set to NULL after release)

Definition at line 164 of file thread.h.

◆ thrRun

#define thrRun (   func,
  name,
  ... 
)     _thrRun(func, name, count_macro_args(__VA_ARGS__), (stvar[]) { __VA_ARGS__ })

void thrRun(threadFunc func, strref name, ...)

Creates and starts a fire-and-forget thread that cannot be waited on or controlled.

Unlike thrCreate(), this does not return a Thread object. The thread runs independently and cannot be joined or explicitly shut down. Use this for truly independent background tasks that don't require coordination.

Parameters
funcThread entry point function
nameThread name for debugging
...Optional arguments passed to the thread function (wrapped as stvar)

Definition at line 66 of file thread.h.

◆ thrSetPriority

#define thrSetPriority (   thread,
  prio 
)    _thrPlatformSetPriority(thread, THREAD_##prio)

bool thrSetPriority(Thread *thread, priority)

Set the scheduling priority of a thread using a ThreadPriority enum value.

Parameters
threadThread object
prioPriority level (e.g., Normal, High, Realtime) - do not prefix with THREAD_
Returns
true if priority was set successfully

Definition at line 146 of file thread.h.

◆ thrSetPriorityV

#define thrSetPriorityV (   thread,
  prio 
)    _thrPlatformSetPriority(thread, prio)

bool thrSetPriorityV(Thread *thread, int prio)

Set the scheduling priority of a thread using a raw integer value.

Parameters
threadThread object
prioRaw priority value (THREAD_Normal, THREAD_High, etc.)
Returns
true if priority was set successfully

Definition at line 154 of file thread.h.

Enumeration Type Documentation

◆ ThreadPriority

Thread scheduling priority levels.

Enumerator
THREAD_Normal 

Normal priority (default)

THREAD_Batch 

Batch processing priority (below normal)

THREAD_Low 

Low priority.

THREAD_Idle 

Idle priority (only runs when system is idle)

THREAD_High 

High priority.

THREAD_Higher 

Higher priority.

THREAD_Realtime 

Real-time priority (use with caution)

Definition at line 15 of file thread.h.

Function Documentation

◆ thrCurrent()

Thread * thrCurrent ( void  )

Get the current thread object

Returns the Thread object for the currently executing thread. This only works for threads created through the CX threading system.

Returns
Thread object for the current thread, or NULL if called from a non-CX thread

◆ thrCurrentOSThreadID()

intptr thrCurrentOSThreadID ( void  )

Get the OS-specific thread ID for the currently executing thread

Unlike thrCurrent(), this function works even on threads not created by CX.

Returns
Platform-specific thread identifier for the current thread

◆ thrLoop()

bool thrLoop ( Thread *  thread)
inline

Check if a thread should continue its main loop

This is the standard pattern for thread main loops. Returns false when another thread has called thrRequestExit() or thrShutdown() on this thread.

Example:

int workerThread(Thread *self) {
while (thrLoop(self)) {
// do work
// OPTIONAL: if idle, wait on notification event
eventWait(self->notify);
}
return 0;
}
bool thrLoop(Thread *thread)
Definition thread.h:96
void eventWait(Event *e)
Definition event.h:160
Parameters
threadThread object (usually the 'self' parameter)
Returns
true if the thread should continue running, false if it should exit

Definition at line 96 of file thread.h.

◆ thrOSThreadID()

intptr thrOSThreadID ( Thread *  thread)

Get the OS-specific thread ID for a thread

Parameters
threadThread object
Returns
Platform-specific thread identifier

◆ thrRegisterSysThread()

void thrRegisterSysThread ( Thread *  thread)

Register a thread as a system thread

System threads are background threads that run independently of the main program and are typically library-created. They are notified at program exit and given an opportunity to perform cleanup before being destroyed.

This should only be used for infrastructure threads like memory allocators, logging systems, or other low-level services that need special shutdown handling.

Parameters
threadThread object to register as a system thread

◆ thrRequestExit()

bool thrRequestExit ( Thread *  thread)

Request a thread to exit gracefully

Sets the thread's exit flag and signals its notification event. The thread should check this flag using thrLoop() or by checking the requestExit flag directly.

This is a non-blocking request. Use thrWait() or thrShutdown() to wait for the thread to actually finish executing.

Parameters
threadThread object to signal
Returns
true if the exit was requested successfully, false if the thread was NULL or already stopped

◆ thrRunning()

bool thrRunning ( Thread *  thread)
inline

Check if a thread is currently running

Parameters
threadThread object
Returns
true if the thread is currently executing, false if it has exited

Definition at line 72 of file thread.h.

◆ thrShutdown()

bool thrShutdown ( Thread *  thread)

Request a thread to exit and wait for it to finish

Combines thrRequestExit() and thrWait() with a 30-second timeout. This is the standard way to cleanly shut down a thread.

Parameters
threadThread object to shut down
Returns
true if the thread exited successfully, false if NULL or timeout expired

◆ thrShutdownMany()

int thrShutdownMany ( sa_Thread  threads)

Shut down multiple threads efficiently in parallel

Requests all threads to exit simultaneously, then waits for them to finish. This is more efficient than calling thrShutdown() on each thread sequentially because all threads can be shutting down in parallel.

Parameters
threadsArray of thread objects to shut down
Returns
Number of threads that successfully shut down within the timeout

◆ thrWait()

bool thrWait ( Thread *  thread,
int64  timeout 
)

Wait for a thread to finish executing

Blocks the calling thread until the specified thread exits or the timeout expires.

Parameters
threadThread object to wait on
timeoutMaximum time to wait in nanoseconds (use timeFromSeconds(), timeFromMs(), etc.)
Returns
true if the thread exited within the timeout, false if timeout expired