JUCE ThreadPool threads waking up every 500ms


#1

Hi,

In ThreadPool::ThreadPoolThread the threads wake up every 500ms due to the timeout set on the WaitableEvent. I am unsure why there is this timeout, can’t we set the timeout to infinite (-1) while waiting for the WaitableEvent?

class ThreadPool::ThreadPoolThread : public Thread
{
public:
ThreadPoolThread (ThreadPool& p)
: Thread (“Pool”), currentJob (nullptr), pool §
{
}

void run() override
{
    while (! threadShouldExit())
        if (! pool.runNextJob (*this))
            wait (500); // Set this to wait (-1) ???
}
ThreadPoolJob* volatile currentJob;
ThreadPool& pool;
JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (ThreadPoolThread)

};

Thanks


#2

I was going to say “it needs to do that so it can check if any new jobs are waiting”, but after looking through the code it can be seen that ThreadPool::addJob calls notify() on all threads in the pool which should accomplish the same thing without the thread checking conditions every 500ms. Looking through the implementation of the ThreadPool class it seems like there’s quite a few arbitrary timeout/wait constants scattered throughout.

Paging @jules, what was the methodology behind this?


#3

Is the code causing some kind of problem?

It’s written like that as a fail-safe. When you have this many threads and signals interacting, it’s almost impossible to be 100% confident there’s no edge-case bug where a signal may have been missed. By having a time-out like this, it ensures that if there was such a bug, the worst case would be a bit of a delay before the action starts. The downside is an unmeasurably short wake up every half-second.