Module std::semaphore

Counting semaphore for concurrency control.

Limit the number of concurrent actors or tasks that can access a shared resource.

Examples

import std::semaphore;

fn main() {
    let sem = semaphore.new(3);
    sem.acquire();
    // critical section
    sem.release();
    sem.free();
}

Contents

Functions

Function new

pub fn new(count: i64) -> Semaphore

Create a new counting semaphore with the given number of permits.

Examples

let sem = semaphore.new(5);

Types

Struct Semaphore

An opaque counting semaphore handle.

Created by semaphore.new(count). Must be freed with free() when no longer needed to avoid leaking memory.

Traits

Trait SemaphoreMethods

Methods available on a Semaphore.

Methods

fn acquire(self: Self)

Block until a permit is available, then acquire it.

fn try_acquire(self: Self) -> bool

Try to acquire a permit without blocking.

Returns true if a permit was acquired, false otherwise.

fn acquire_timeout(self: Self, timeout_ms: i64) -> bool

Try to acquire a permit, waiting up to timeout_ms milliseconds.

Returns true if a permit was acquired before the timeout.

fn release(self: Self)

Release a previously acquired permit.

fn count(self: Self) -> i64

Return the current number of available permits.

fn free(self: Self)

Release the semaphore resources.