|
CX Framework
Cross-platform C utility framework
|
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) |
| #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.
| pthread | Pointer to thread object pointer (set to NULL after release) |
| #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.
| func | Thread entry point function |
| name | Thread name for debugging |
| ... | Optional arguments passed to the thread function (wrapped as stvar) |
| #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.
| thread | Thread object |
| prio | Priority level (e.g., Normal, High, Realtime) - do not prefix with THREAD_ |
| #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.
| thread | Thread object |
| prio | Raw priority value (THREAD_Normal, THREAD_High, etc.) |
| enum ThreadPriority |
Thread scheduling priority levels.
| 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.
| 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.
|
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:
| thread | Thread object (usually the 'self' parameter) |
| intptr thrOSThreadID | ( | Thread * | thread | ) |
Get the OS-specific thread ID for a thread
| thread | Thread object |
| 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.
| thread | Thread object to register as a system thread |
| 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.
| thread | Thread object to signal |
|
inline |
| 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.
| thread | Thread object to shut down |
| 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.
| threads | Array of thread objects to shut down |
| 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.
| thread | Thread object to wait on |
| timeout | Maximum time to wait in nanoseconds (use timeFromSeconds(), timeFromMs(), etc.) |