Using TBB for non-parallel tasks

I want to get the thread pool behavior using TBB. But whenever I read documents on TBB, they always talk about parallel, parallel-dowhile, etc. On the contrary, I need a main thread to assign tasks to a thread pool, so that these tasks are performed "independently" - to perform tasks asynchronously. The tasks here may be event handling for the graphical interface.

Is TBB Task Scheduler suitable for this behavior? The impression I got from the task scheduler is that it is only useful if I have tasks that can be broken down and run in parallel to each other.

+4
source share
1 answer

Starting with version 3.0, TBB supports asynchronous task execution. To do this, a special tbb::task::enqueue() sentence method was added. Unlike tbb::task::spawn() , this method guarantees the execution of the given queue, even if the source thread never enters a method for sending the task, for example, wait_for_all() .

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 mentioned in a comment by @JimMishell, an example of using this function to handle GUI events can be found in "Design Patterns"; and a formal description of the method is available in the reference manual (see TBB documentation for both).

+9
source

Source: https://habr.com/ru/post/1383509/


All Articles