I'm seeing a sporadic hang when my application loads. As far as I can work out the sequence of events is as follows and I was wondering if there is a recommended way to avoid this occuring.
1. App starts 2. It loads dll 'Foo' (no code in dll main) 3. Foo has dependency on bar so it causes dll 'Bar' to load 4. App now calls initialise function in Foo 4.1 Foo's initalise function calls initialise in Bar 4.2 Bar starts a background thread (license check) 4.3 Foo creates tbb::task_scheduler_init, deferred 4.4 Foo calls tbb::task_scheduler_init::initialize(...) -- hang -- function never returns
This main thread of execution is stuck in tbbmalloc.cpp:144, in a call to LoadLibrary, waiting for the global dll lock. This has the comment line. "Preventing TBB allocator library from unloading to prevent resource leak, as memory is not released on the library unload."
The interesting bit is that the other thread that was fired off by the 'Bar' dll has now got stuck in mallocThreadShutdownNotification, MemoryAllocator.cpp. It has the global dll lock as this was triggered by Dllmain in tbbmalloc.cpp:215 (DLL_THREAD_DETACH), but is not able to get tbb's lock.
Any suggestions, maybe some finer grain control on startup could be useful. For now I think I will just make sure tbb is initialised as early as possible, even if other tasks are unrelated. However this still does not seem like it would entirely prevent this situation, as the dll main events will presumbly start firing as soon as the tbb dll is loaded.