using TBB for non-parallel tasks

隐身守侯 提交于 2019-12-04 10:26:15
Alexey Kukanov

Starting from version 3.0, TBB has support for asynchronous execution of tasks. For that, a special work offering method tbb::task::enqueue() was added. Unlike tbb::task::spawn(), this method guarantees that the enqueued task will be executed even if the originating thread never enters a task dispatch method such as wait_for_all().

A short usage example for task::enqueue():

class MyTask : public tbb::task {
    /*override*/ tbb::task* execute() {
        // Do the job
        return NULL; // or a pointer to a new task to be executed immediately
    }
};

MyTask* t = new (tbb::task::allocate_root()) MyTask();
tbb::task::enqueue(*t);
// Do other job; the task will be executed asynchronously

As @JimMishell mentioned in the comment, an example how to use it for handling of GUI events can be found in "Design Patterns"; and the formal description of the method is available in the Reference manual (see TBB documentation for both).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!