pub struct Semphore { /* private fields */ }
Expand description
Semphore primitive
semaphores allow threads and coroutines to synchronize their actions.
A semaphore is an integer whose value is never allowed to fall below zero. Two operations can be performed on semaphores: increment the semaphore value by one (post()); and decrement the semaphore value by one (wait()). If the value of a semaphore is currently zero, then a wait() operation will block until the value becomes greater than zero.
§Examples
use std::sync::Arc;
use may::coroutine;
use may::sync::Semphore;
let sem = Arc::new(Semphore::new(0));
let sem2 = sem.clone();
// spawn a coroutine, and then wait for it to start
unsafe {
coroutine::spawn(move || {
sem2.post();
});
}
// wait for the coroutine to start up
sem.wait();
Implementations§
source§impl Semphore
impl Semphore
sourcepub fn wait(&self)
pub fn wait(&self)
wait for a semphore
if the semphore value is bigger than zero the function returns immediately
otherwise it would block the until a post
is executed
sourcepub fn wait_timeout(&self, dur: Duration) -> bool
pub fn wait_timeout(&self, dur: Duration) -> bool
same as wait
except that with an extra timeout value
return false if timeout happened
sourcepub fn try_wait(&self) -> bool
pub fn try_wait(&self) -> bool
return false if would block return true if successfully acquire one semphore resource