Is there any way to prevent tbb from activating new threads beyond those initially spawned at the start of a job? This seems to be a "newish" feature. I've noticed that if I configure a thread pool of size N, and start a job, after a while new threads will suddenly appear (by monitoring the thread id). While there are never more than N active threads, new threads are being created, and old ones retired. With older versions of tbb, this wasn't the case.
This is a big problem for our code base, as we need to do a lot of thread local initialization in each thread before individual tasks can run correctly. We can get around this by using a thread local bool to mark if initialization has been done and checking it at the start of each task::execute. If it's not set, the thread local init is performed. But it's a waste of resources to have to do this - eg even on some small jobs with a pool of size 4, I see 39 different threads being created over the course of the job (this is on a machine with 40 cores - I wonder if it's related).
A further concern is that at the end of the job, thread local termination needs to be done in each active thread. I have occasionally seen issues where threads have been activated so late in the job that tbb::tasks were no longer being submitted to the queue, and thus the thread local initialization was never triggered, which caused the thread local termination to do very bad things.
Is there any way to detect when a thread has been retired from the pool? I was hoping that the tbb::task_scheduler_observer would help here, but that doesn't seem to be the case.