Module thread
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
| SpawnedThread | A running thread started by thread_spawn, and its result. A Drop value: join() it (or drop() it, which joins and drops the result). |
| Mutex | A T shared between threads, reachable only while locked. Moving a Mutex after locking it is not allowed (guards point at it). |
| MutexGuard | Proof of holding a Mutex's lock: get() is the protected value, valid until the guard is dropped (which unlocks). |
| RwLock | Like 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_create | Starts 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_trylock | 0=locked, non-zero=busy |
| mutex_unlock | Don'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_init | cond_wait atomically releases mutex and sleeps; re-acquires on wake. |
| cond_destroy | |
| cond_wait | |
| cond_timedwait_ms | |
| cond_signal | |
| cond_broadcast | |
| rwlock_init | Multiple simultaneous readers; exclusive writers. |
| rwlock_destroy | |
| rwlock_rdlock | |
| rwlock_wrlock | |
| rwlock_tryrdlock | |
| rwlock_trywrlock | |
| rwlock_rdunlock | |
| rwlock_wrunlock | |
| thread_spawn | Runs f(arg) on a new thread. 'arg' moves into it (using it afterwards is an error); T and R must be Send. |
Macros
| THREAD_TIMEDOUT | Waits at most 'ms' milliseconds: 0 if woken, THREAD_TIMEDOUT if the time ran out, -1 on another error (the same codes on every platform). |