semaphore
Generated by tinox doc
Three synchronization primitives for coordinating spawned threads: Mutex (exclusive lock), RWLock (multiple readers or one writer), and Semaphore (a counted permit pool).
Mutex
import tinox.core.semaphore;
class Main
{
fnc main() -> Int32
{
let mutex: Mutex = Mutex::new();
Mutex::lock(mutex);
println("critical section");
Mutex::unlock(mutex);
if Mutex::tryLock(mutex)
{
println("acquired again");
Mutex::unlock(mutex);
}
return 0;
}
}
Semaphore
import tinox.core.semaphore;
class Main
{
fnc main() -> Int32
{
// At most 2 concurrent permits.
let sem: Semaphore = Semaphore::new(2);
Semaphore::acquire(sem);
Semaphore::acquire(sem);
println("has " + sem.count.toString() + " permits left");
if Semaphore::tryAcquire(sem) == false
{
println("no permits available");
}
Semaphore::release(sem);
println("has " + sem.count.toString() + " permits left");
return 0;
}
}
class Mutex
Fields
locked | Bool | |
queue | List<Int64> |
Methods
fn new() → Mutex
fn lock(mutex: Mutex) → Nothing
fn unlock(mutex: Mutex) → Nothing
fn tryLock(mutex: Mutex) → Bool
class RWLock
Fields
readers | Int64 | |
writers | Int64 |
Methods
fn new() → RWLock
fn readLock(lock: RWLock) → Nothing
fn readUnlock(lock: RWLock) → Nothing
fn writeLock(lock: RWLock) → Nothing
fn writeUnlock(lock: RWLock) → Nothing
class Semaphore
Fields
count | Int64 | |
waiting | List<Int64> |
Methods
fn new(count: Int64) → Semaphore
fn acquire(sem: Semaphore) → Nothing
fn release(sem: Semaphore) → Nothing
fn tryAcquire(sem: Semaphore) → Bool