Main Content

wait

Wait for futures to complete

    Description

    example

    wait(F) suspends MATLAB® execution until each element of the Future array F is finished. When the function associated with a Future object finishes running, MATLAB sets the State property of the Future to 'finished'.

    wait(F,state) blocks MATLAB until each element of the Future array F has its State property set to state.

    tf = wait(F,state,timeout) blocks execution for a maximum of timeout seconds.

    If each element of the Future array F finishes before timeout seconds elapse, tf is true. Otherwise, tf is false.

    Examples

    collapse all

    Run a function in the background, then wait for the function to finish running.

    Use parfeval to run pause(5) without retrieving any outputs. Specify backgroundPool as the first argument to run the function in the background. When you use parfeval, you create a Future object.

    f = parfeval(backgroundPool,@pause,0,5);

    Check the state of the Future.

    f.State
    ans = 'running'

    When you run parfeval, you schedule a function to run in the background. When the background pool has insufficient available resources to run the function, the Future is in the 'queued' state. When the function is run by the background pool, the Future is in the 'running' state.

    Wait for the function to finish running in the background. When you wait for the function to finish running, you block MATLAB until the function finishes running.

    wait(f)
    f.State
    ans = 'finished'

    The function is now in the 'finished' state.

    Input Arguments

    collapse all

    Input Future, specified as a parallel.Future scalar or array.

    State to wait for, specified as "finished" or "running".

    Seconds to wait for, specified as a real numeric scalar.

    Example: timeout = 5;

    Example: timeout = single(3.14);

    Version History

    Introduced in R2013b