std1.0.0-beta-7 SafeC docs

Module thread

#include <std/thread.h> source

Thread Cross-platform: POSIX pthreads (Linux/macOS) or Win32 (Windows). Backend is selected automatically from '_WIN32', which safec auto-defines for a Windows host/target (see main.cpp's platform-detection block) — same convention as std/stderr_compat.h.

Thread handles: unsigned long long POSIX → pthread_t value (8 bytes on 64-bit) Win32 → HANDLE (kernel object pointer, 8 bytes on x64)

Mutex / condvar / rwlock handles: unsigned long long POSIX → pointer to heap-allocated pthread object Win32 → pointer to heap-allocated Windows primitive

Structs

SpawnedThreadA running thread started by thread_spawn, and its result. A Drop value: join() it (or drop() it, which joins and drops the result).
MutexA T shared between threads, reachable only while locked. Moving a Mutex after locking it is not allowed (guards point at it).
MutexGuardProof of holding a Mutex's lock: get() is the protected value, valid until the guard is dropped (which unlocks).
RwLockLike Mutex<T>, with many readers at once (const access) or one writer. Readers share the value, so T must be Sync as well as Send.
RwReadGuard
RwWriteGuard

Functions

thread_createStarts func(arg) on a new thread and stores its handle in *tid. 0 on success. func's return value is discarded (thread_join only waits). static ?&Job work(?&Job j) { ... return null; } unsigned long long tid = 0ULL; std::thread_create(&tid, work, (?&Job)&job);
thread_join
thread_detach
thread_yield
thread_sleep_ms
thread_self
mutex_init
mutex_destroy
mutex_lock
mutex_trylock0=locked, non-zero=busy
mutex_unlockDon't re-lock a mutex the calling thread already holds: a Windows CRITICAL_SECTION allows it (recursive), a default POSIX mutex deadlocks (mutex_lock) or reports busy (mutex_trylock).
cond_initcond_wait atomically releases mutex and sleeps; re-acquires on wake.
cond_destroy
cond_wait
cond_timedwait_ms
cond_signal
cond_broadcast
rwlock_initMultiple simultaneous readers; exclusive writers.
rwlock_destroy
rwlock_rdlock
rwlock_wrlock
rwlock_tryrdlock
rwlock_trywrlock
rwlock_rdunlock
rwlock_wrunlock
thread_spawnRuns f(arg) on a new thread. 'arg' moves into it (using it afterwards is an error); T and R must be Send.

Macros

THREAD_TIMEDOUTWaits at most 'ms' milliseconds: 0 if woken, THREAD_TIMEDOUT if the time ran out, -1 on another error (the same codes on every platform).