Startseite › Sprach-Frontends › C Frontend › Übersicht Hilfe zum PBXB64-Compiler

PBXB64 - C11 Threads

PBXB64 provides a subset of <threads.h> implemented on top of the Win32 thread primitives already used by the PB runtime.

Kategorie: Übersicht

Beschreibung

PBXB64 provides a subset of <threads.h> implemented on top of the Win32 thread primitives already used by the PB runtime.

Supported API

NameDescription
thrd_tThread handle (64-bit)
thrd_createCreate a thread
thrd_joinWait for thread and release handle
thrd_exitExit current thread
mtx_tMutex handle
mtx_initInitialize mutex (mtx_plain only)
mtx_lockAcquire mutex
mtx_unlockRelease mutex
mtx_destroyDestroy mutex

Thread/Mutex Handles

Local handles may produce incorrect argument passing.

Limitation: thrd_detach, thrd_sleep, thrd_yield, mtx_timed, mtx_recursive, condition variables, and tss_* are not yet implemented.

Back to C Language Reference

Beispiel

#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;
}