X_THREADING_VERSION_MAJOR
#define X_THREADING_VERSION_MAJOR 1
STDX - Multithreading Utilities Part of the STDX General Purpose C Library by marciovmf License: MIT https://github.com/marciovmf/stdx
Designed to abstract platform-specific APIs (e.g., pthreads, Win32) behind a consistent and lightweight int32_terface. This header provides functions for:
To compile the implementation define X_IMPL_THREAD
in one source file before including this header.
To customize how this module allocates memory, define
X_THREAD_ALLOC / X_THREAD_FREE before including.
#define X_THREADING_VERSION_MAJOR 1
#define X_THREADING_VERSION_MINOR 0
#define X_THREADING_VERSION_PATCH 0
#define X_THREADING_VERSION(X_THREADING_VERSION_MAJOR *10000+X_THREADING_VERSION_MINOR *100+X_THREADING_VERSION_PATCH)
typedef void(*XThreadTask)(void *arg);
typedef void *(*XThreadFunc)(void *);
typedef struct XThread XThread;
typedef struct XMutex XMutex;
typedef struct XCondVar XCondVar;
typedef struct XThreadPool XThreadPool;
typedef struct XTask XTask;
Create and start a new thread.
int32_t x_thread_create(
XThread **t,
XThreadFunc func,
void *arg
);
XThread **tXThreadFunc funcvoid *arg0 on success, non-zero on failure.
Wait for a thread to finish execution.
void x_thread_join(XThread *t);
XThread *tDestroy a thread handle and release its resources.
void x_thread_destroy(XThread *t);
XThread *tCreate a mutex.
int32_t x_thread_mutex_init(XMutex **m);
XMutex **m0 on success, non-zero on failure.
Lock a mutex, blocking until it becomes available.
void x_thread_mutex_lock(XMutex *m);
XMutex *mUnlock a mutex.
void x_thread_mutex_unlock(XMutex *m);
XMutex *mDestroy a mutex and release its resources.
void x_thread_mutex_destroy(XMutex *m);
XMutex *mCreate a condition variable.
int32_t x_thread_condvar_init(XCondVar **cv);
XCondVar **cv0 on success, non-zero on failure.
Wait on a condition variable.
void x_thread_condvar_wait(
XCondVar *cv,
XMutex *m
);
Wake one thread waiting on a condition variable.
void x_thread_condvar_signal(XCondVar *cv);
XCondVar *cvWake all threads waiting on a condition variable.
void x_thread_condvar_broadcast(XCondVar *cv);
XCondVar *cvDestroy a condition variable and release its resources.
void x_thread_condvar_destroy(XCondVar *cv);
XCondVar *cvSleep the current thread for at least the given number of milliseconds.
void x_thread_sleep_ms(int ms);
int msYield execution to allow other threads to run.
void x_thread_yield(void);
Create a thread pool with a fixed number of worker threads.
XThreadPool * x_threadpool_create(int num_threads);
int num_threadsThread pool handle, or NULL on failure.
Enqueue a task for execution by the thread pool.
int32_t x_threadpool_enqueue(
XThreadPool *pool,
XThreadTask fn,
void *arg
);
XThreadPool *poolXThreadTask fnvoid *arg0 on success, non-zero on failure.
Destroy a thread pool and release its resources.
void x_threadpool_destroy(XThreadPool *pool);
XThreadPool *poolInternal macro for allocating memory. To override how this header allocates memory, define this macro with a different implementation before including this header.
#define X_THREAD_ALLOC(sz) malloc(sz)
szInternal macro for freeing memory. To override how this header frees memory, define this macro with a different implementation before including this header.
#define X_THREAD_FREE(p) free(p)
pstruct XThreadWrapper{
XThreadFunc func;
void *arg;
};
int32_t x_thread_mutexinit(XMutex **m);
#define THREADPOOL_MAGIC 0xDEADBEEF