0
votes

I'm working with TBB for the first time and want to run a sequence of tasks that are all independent. I wanted to use tbb::task::enqueue, i.e. I create a root task and it creates a child task that is independent of the parent. This child can create a task again and so on.

My solution does not work, because the child task calls the parent again and I get an endless cascade. The execution of the root task works, but as soon as a child is created, there is an endless cascade.


// initialization
tbb::task::enqueue( *new( tbb::task::allocate_root() ) tbbwrapper::ctask( <some data> ) );

// execute method
tbb::task* ctask::execute()
{
    <execute data processing>

    return <data result correct> 
    ? new( this->allocate_child() ) tbbwrapper::ctask( <result data> ) 
    : NULL;
}

I have a lot of tasks (all are independent and the execution order is also completely independent) to run and each task can create a successor, but it is completely undependent and just needs to be added to the queue to run. Also I don't need Wait or similar, because the execution should run in the background.

I just need a simple example to show me how this works.

1
If your tasks are all independent, do not create child tasks; create a new "root" task instead and enqueue it. Also note that the tasks submitted via task::enqueue are scheduled via a shared work queue, not via work stealing. - Alexey Kukanov

1 Answers

0
votes

Usually, it is not recommended to use TBB task API directly because it is over-complicated and contains a lot of hidden problems. If you know the number of the tasks in advance, you may want to use tbb::parallel_for. If you want to add tasks during algorithm execution, you can use feeder functionality of tbb::parallel_do. If you do not want to use algorithms and want to manipulate with tasks, consider using tbb::task_group.

Do not you need to wait for background work completion even when the application ends? You can use tbb::task_group to create a background task, e.g.

tbb::task_group tg;
// Run parallel_for in background.
tg.run( [] { 
    tbb::parallel_for(...);
} );
// ...
// Any other code
// ...
tg.wait(); // task_group requires to call `wait` to guarantee that all tasks are really processed

If you want to guarantee a concurrent execution of background work, you can enqueue the only one task that will create additional work with other TBB algorithms.

[UPDATE]

Consider the run_task helper that runs a function in background.

#include "tbb/task.h"
#include <iostream>
#include <thread>

template <typename F>
class task_wrapper : public tbb::task {
    F f_;
public:
    task_wrapper(F f) : f_(f) {}
    tbb::task* execute() override {
        f_();
        return NULL;
    }
};

template <typename F>
void run_task(F f) {
    tbb::task::enqueue(*new(tbb::task::allocate_root()) task_wrapper<F>(f));
}

int main() {
    for (int i = 0; i < 10; ++i) {
        run_task([i] {
            std::cout << i << std::endl;
        });
    }

    // Give time to proceed the background tasks.
    using namespace std::chrono_literals;
    std::this_thread::sleep_for(1s);

    return 0;
}