vera:thread
Threading and synchronization.
import "vera:thread"
Overview
Provides thread creation/joining, mutexes, and sleep. All functions are built on compiler primitives (__vera_sys_*) which the compiler maps to platform-specific threading APIs (pthreads on Linux/macOS, Win32 threads on Windows).
Dependencies: None (uses primitives directly)
Functions
Thread Operations
thread_spawn
function thread_spawn(func: ptr, arg: ptr) -> i64
Spawn a new thread running the given function with an argument. The function must have the signature function(arg: ptr) -> i64.
Parameters:
func(ptr) — pointer to the thread entry functionarg(ptr) — argument to pass to the function
Returns: a thread handle used for joining
thread_join
function thread_join(handle: i64) -> i64
Wait for a thread to finish and return its exit value.
Parameters:
handle(i64) — thread handle returned bythread_spawn
Returns: the thread's return value
thread_sleep
function thread_sleep(milliseconds: i64) -> i32
Sleep the current thread for the given number of milliseconds.
Parameters:
milliseconds(i64) — duration to sleep
Returns: 0 on success
Mutex Operations
mutex_create
function mutex_create() -> i64
Create a new mutex.
Returns: a mutex handle
mutex_lock
function mutex_lock(handle: i64) -> i32
Lock a mutex. Blocks until the mutex is acquired.
Parameters:
handle(i64) — mutex handle returned bymutex_create
Returns: 0 on success
mutex_trylock
function mutex_trylock(handle: i64) -> i32
Try to lock a mutex without blocking.
Parameters:
handle(i64) — mutex handle returned bymutex_create
Returns: 0 if acquired, non-zero if the mutex is already locked
mutex_unlock
function mutex_unlock(handle: i64) -> i32
Unlock a mutex.
Parameters:
handle(i64) — mutex handle returned bymutex_create
Returns: 0 on success
mutex_destroy
function mutex_destroy(handle: i64) -> i32
Destroy a mutex and free its resources.
Parameters:
handle(i64) — mutex handle returned bymutex_create
Returns: 0 on success