Intel® oneAPI Threading Building Blocks
Ask questions and share information about adding parallelism to your applications when using this threading library.
This community is designed for sharing of public information. Please do not share Intel or third-party confidential information here.

Wait until all tasks are complete?

Is there a way to wait until all tasks associated with a givenschedulerare complete?
My app is currently crashing on shutdown because I am unloading DLL's which TBB tasks are actively using. I tried calling terminate() on mytbb::task_scheduler_init object prior to unloading these DLL's but this doesn't seem to help. Putting in a Sleep(500) does fix the problem, but it is just a dirty hack.
0 Kudos
1 Reply
For now, there is no better way to handle this than putting a sleep.

It's not because of the tasks still working. The root cause is that TBB worker threads are finished asynchronuosly, i.e. the thread that initiated shutdown does not wait for TBB worker threads to complete. We do that because otherwise we experienced startup/shutdown problems related to so called loader lock.

You can use task_scheduler_observer to check that all worker threads entered shutdown mode (they should call on_scheduler_exit method for the registered observers). After that, you will still need to sleep before unloading, to accomodate for threads finishing execution. While you could obtain thread handles via the observer, I would not recommend waiting on those handles unless you know for sure that the loader lock is not acquired by the main thread. I.e. do not wait in DllMain, or in a destructor of a static object, etc - but if you have a special function that should be called to initiated module unload, you could probably try waiting for thread completion inside this function.