Condition

Condition()

Create an edge-triggered event source that tasks can wait for. Tasks that call wait on a Condition are suspended and queued. Tasks are woken up when notify is later called on the Condition. Edge triggering means that only tasks waiting at the time notify is called can be woken up. For level-triggered notifications, you must keep extra state to keep track of whether a notification has happened. The Channel type does this, and so can be used for level-triggered events.

Examples

julia> using Base.Threads

julia> cond = Condition();

julia> task1 = Threads.@spawn begin
           println("Task 1: Waiting for notification")
           wait(cond)
           println("Task 1: Woken up!")
       end;

julia> task2 = Threads.@spawn begin
           println("Task 2: Waiting for notification")
           wait(cond)
           println("Task 2: Woken up!")
       end;

julia> Threads.@spawn begin
           sleep(1)
           println("Notifying tasks...")
           notify(cond)
       end;

Task 1: Waiting for notification
Task 2: Waiting for notification
Notifying tasks...
Task 1: Woken up!
Task 2: Woken up!

In this example, we create a Condition object cond. We then spawn two tasks, task1 and task2, which both call wait on the Condition. The tasks are suspended and queued until the Condition is notified. After waiting for 1 second, we call notify(cond) to wake up all tasks waiting on the Condition. Both task1 and task2 are then woken up and resume execution.

Note that Condition is typically used in conjunction with multi-threading to coordinate the execution of tasks.

See Also

:@async, :@schedule, :@task, Condition, consume, interrupt, istaskdone, istaskstarted, lock, notify, ReentrantLock, schedule, Task, task_local_storage, unlock, wait, yield, yieldto,

User Contributed Notes

Add a Note

The format of note supported is markdown, use triple backtick to start and end a code block.

*Required Field
Details

Checking you are not a robot: