Resumable tasks


Functions to suspend task execution at a specific point and signal to resume it later.

// Defined in header <tbb/task.h>

using tbb::task::suspend_point = /* implementation-defined */;
template < typename Func > void tbb::task::suspend( Func );
void tbb::task::resume( tbb::task::suspend_point );


The tbb::task::suspend function called within a running task suspends execution of the task and switches the thread to participate in other oneTBB parallel work. This function accepts a user callable object with the current execution context tbb::task::suspend_point as an argument.

The tbb::task::suspend_point context tag shall be passed to the tbb::task::resume function to trigger a program execution at the suspended point. The tbb::task::resume function can be called at any point of an application, even on a separate thread. In this regard, this function acts as a signal for the task scheduler.


Note, that there are no guarantees, that the same thread that called tbb::task::suspend will continue execution after the suspended point. However, these guarantees are provided for the outermost blocking oneTBB calls (such as tbb::parallel_for and tbb::flow::graph::wait_for_all) and tbb::task_arena::execute calls.


// Parallel computation region
tbb::parallel_for(0, N, [&](int) {
    // Suspend the current task execution and capture the context
    tbb::task::suspend([&] (tbb::task::suspend_point tag) {
        // Dedicated user-managed activity that processes async requests.
        async_activity.submit(tag); // could be OpenCL/IO/Database/Network etc.
    }); // execution will be resumed after this function
// Dedicated user-managed activity:

// Signal to resume execution of the task referenced by the tbb::task::suspend_point
// from a dedicated user-managed activity