Community
cancel
Showing results for 
Search instead for 
Did you mean: 
oliver_critchley
Beginner
274 Views

How to spawn a task without waiting for it to complete

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
261 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


Mark_L_Intel
Employee
248 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.


oliver_critchley
Beginner
107 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!

Alexei_K_Intel
Employee
217 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.

oliver_critchley
Beginner
181 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...

Alexei_K_Intel
Employee
172 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.

oliver_critchley
Beginner
159 Views

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