notify

notify(condition, val=nothing; all=true, error=false)

Wake up tasks waiting for a condition, passing them val. If all is true (the default), all waiting tasks are woken, otherwise only one is. If error is true, the passed value is raised as an exception in the woken tasks.

Examples

  1. Wake up all waiting tasks with a value:

    julia> condition = Condition();
    julia> task1 = @async begin
               wait(condition);
               println("Task 1 executed");
           end;
    julia> task2 = @async begin
               wait(condition);
               println("Task 2 executed");
           end;
    julia> notify(condition, "Value", all=true, error=false);
    Task 1 executed
    Task 2 executed

    In this example, both task1 and task2 are waiting on the condition. When notify is called with all=true and val="Value", both tasks wake up and execute.

  2. Wake up a single waiting task with a value:

    julia> condition = Condition();
    julia> task1 = @async begin
                wait(condition);
                println("Task 1 executed");
            end;
    julia> task2 = @async begin
                wait(condition);
                println("Task 2 executed");
            end;
    julia> notify(condition, "Value", all=false, error=false);
    Task 1 executed

    In this example, only task1 is woken up and executed because all=false was specified in the notify call.

  3. Wake up tasks and raise an exception:
    julia> condition = Condition();
    julia> task1 = @async begin
                wait(condition);
                println("Task 1 executed");
            end;
    julia> task2 = @async begin
                wait(condition);
                println("Task 2 executed");
            end;
    julia> notify(condition, ArgumentError("Invalid argument"), all=true, error=true);
    ERROR: ArgumentError: Invalid argument
    ...

    Here, both tasks are woken up by calling notify with all=true, and an ArgumentError exception is raised in each task.

Common mistake example:

julia> condition = Condition();
julia> notify(condition, "Value", all=1, error=false);
ERROR: MethodError: no method matching notify(::Condition, ::String; all=1, error=false)

In this example, the all argument is given a value of 1 instead of a boolean value (true or false). Ensure that the all argument is provided with the correct boolean value to avoid such errors.

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: