struct Syn::Core::ConditionVariable

Overview

Synchronize execution of concurrently running fibers.

This can be used to replace polling with a waiting list that can be resumed when resources are available, while still behaving inside a mutually exclusive context: when a waiting fiber is resumed, the lockable will be locked.

Can also be used as a notification system without a lockable. In which case the lockable must be nil (or Pointer(Syn::Lockable).null) and the lockable won't be unlocked nor locked.

Defined in:

core/condition_variable.cr

Constructors

Instance Method Summary

Constructor Detail

def self.new #

[View source]

Instance Method Detail

def broadcast : Nil #

Enqueues all waiting fibers at once. Does nothing if there aren't any waiting fiber.


[View source]
def signal : Nil #

Enqueues one waiting fiber. Does nothing if there aren't any waiting fiber.


[View source]
def wait(lockable : Pointer(Lockable) | Nil, timeout : Time::Span, *, relock_on_timeout : Bool = true) : Bool #

Identical to #wait but the current fiber will be resumed automatically when timeout is reached. Returns true if the timeout was reached, false otherwise.

EXPERIMENTAL The timeout feature is experimental.


[View source]
def wait(lockable : Pointer(Lockable) | Nil) : Nil #

Suspends the current fiber. The lockable is unlocked before the fiber is suspended (the current fiber must be holding the lock) and will be locked again after the fiber is resumed and before the function returns.


[View source]