Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
2466 Discussions

How to spawn a task without waiting for it to complete

oliver_critchley
Beginner
2,574 Views

Hi,

If I want to enqueue a "fire and forget" task I can use task_arena::enqueue().

But if I want to spawn a "fire and forget" task things seem a lot harder. Previously I could use task::spawn(), but that's been removed. task_group::run() will spawn a task, but then I must wait for it to complete before I can destroy the task group.

Is there any way to spawn a task without being forced to wait for it to complete?

Thanks!

0 Kudos
7 Replies
GouthamK_Intel
Moderator
2,561 Views

Hi,

Thanks for reaching out to us!

We are escalating this thread to the Subject Matter Expert (SME) who will guide you further.

Have a Good day!


Thanks & Regards

Goutham


0 Kudos
Mark_L_Intel
Moderator
2,548 Views

please look at the relevant discussion here https://github.com/oneapi-src/oneTBB/issues/243 especially closer to the end -- at the bottom -- regarding task spawn. I think you are correct that you are experience the problem indeed due to the change in (TBB) task interface. I will point internally to your question since it deserves more explanations.


0 Kudos
oliver_critchley
Beginner
2,407 Views

Thanks for your reply. I had seen the discussion in issue 243 and the remarks at the end go a little way in the same direction as my question.

I guess my point is about some of the design principles behind TBB:

  • "Spawn" has always been preferred to "enqueue":
    • The arena's task queue is a bottleneck.
    • When a thread picks up an enqueued task the cache is usually cold; for a spawned task the cache is much more likely to be hot.
  • Many of TBB's higher-level algorithms are based on the assumption that you want to perform some work in parallel and then synchronize the results.

My problem is that my application has lot of asynchronous processing, but I'd like to be able to keep getting all the goodness associated with spawning my tasks. If I have to fall back to enqueuing tasks I might as well just use a thread pool

It seems to me that the removal of task::spawn() makes it much harder to use TBB in this class of application, which seems a pity to me. I'd be interested to hear some remarks about this from you guys at Intel.

Thanks!

0 Kudos
Alexei_K_Intel
Employee
2,517 Views

It is not recommended to spawn a task and not to wait it. In that case, such task might be never executed. To guarantee that the task is executed you need to either wait the task or enqueue it. You can use the attached task_arena to enqueue the task to the arena associated with the calling thread:

 

tbb::task_arena arena(tbb::task_arena::attach{});
arena.enqueue([] { ... } );

 

Currently, we are considering the interfaces to simplify task enqueue mechanism.

0 Kudos
oliver_critchley
Beginner
2,481 Views

Thanks for considering my question, and I see your point about the dangers of not waiting for a spawned task.

On the other hand, TBB is a C++ library and one of the reasons I use C++ is because it allows me to choose to do dangerous things. For example, malloc() is dangerous because I might forget to call free() but in C++ I can choose to use it if I want.

So it would be nice if TBB followed the same principle...

0 Kudos
Alexei_K_Intel
Employee
2,472 Views

The malloc case is slightly different because we understand the side effects of missed free: it is just a memory leak and it is acceptable until there is available memory. With the missed wait, you might face a hang/deadlock that the task is never executed.

If you are Ok that tasks might be never executed you can allocate task_group on the heap, run the tasks inside and never wait/destroy it.

0 Kudos
oliver_critchley
Beginner
2,459 Views

OK, I guess that seems to be the way things are then. Thanks for your thoughts!

0 Kudos
Reply