It looks like ThreadPool
just spawns a bunch of threads to be used for running jobs. Couldn’t this be made more (possibly much more) performant by setting the affinity for each thread such that the threads are locked to cores?
This helps with situations where the OS/some other app wants to run something on one core, so the OS boots one of the ThreadPool
threads off to another core, which then runs (or something else runs) and boots another ThreadPool
thread off to another core, and so on, causing a lot of context switches that could be avoided if the threads were just stuck to cores via affinity masks.
i.e. changing
void ThreadPool::createThreads (int numThreads, size_t threadStackSize)
{
for (int i = jmax (1, numThreads); --i >= 0;)
threads.add (new ThreadPoolThread (*this, threadStackSize));
for (auto* t : threads)
t->startThread();
}
to (untested)
void ThreadPool::createThreads (int numThreads, size_t threadStackSize)
{
int numCpus = SystemStats::getNumCpus();
for (int i = jmax (1, numThreads); --i >= 0;)
{
ThreadPoolThread* newThread = new ThreadPoolThread (*this, threadStackSize);
uint32 affinityMask = 1 << (i % numSystemThreads);
newThread->setAffinityMask (affinityMask);
threads.add (newThread);
}
for (auto* t : threads)
t->startThread();
}