PBXB64 provides a subset of <threads.h> implemented on top of the Win32 thread primitives already used by the PB runtime.
PBXB64 provides a subset of <threads.h> implemented on top of the Win32 thread primitives already used by the PB runtime.
| Name | Description |
|---|---|
thrd_t | Thread handle (64-bit) |
thrd_create | Create a thread |
thrd_join | Wait for thread and release handle |
thrd_exit | Exit current thread |
mtx_t | Mutex handle |
mtx_init | Initialize mutex (mtx_plain only) |
mtx_lock | Acquire mutex |
mtx_unlock | Release mutex |
mtx_destroy | Destroy mutex |
Local handles may produce incorrect argument passing.
thrd_detach, thrd_sleep, thrd_yield, mtx_timed, mtx_recursive, condition variables, and tss_* are not yet implemented.#include <threads.h>
#include <stdatomic.h>
static atomic_int counter;
static mtx_t mutex;
static thrd_t worker;
static int thread_fn(void* arg) {
(void)arg;
for (int i = 0; i < 1000; ++i) {
mtx_lock(&mutex);
atomic_fetch_add(&counter, 1);
mtx_unlock(&mutex);
}
return 0;
}
int main(void) {
atomic_init(&counter, 0);
mtx_init(&mutex, mtx_plain);
thrd_create(&worker, thread_fn, NULL);
thrd_join(worker, NULL);
mtx_destroy(&mutex);
return atomic_load(&counter) == 1000 ? 0 : 1;
}