Consider this sequence of events on a ThreadPool with one thread:
- ThreadPool::addJob successfully adds a job to the array of jobs and starts the thread
- before ThreadPool::runNextJob gets to run, someone calls ThreadPoolJob::signalJobShouldExit on the just-added job
- when ThreadPool::runNextJob gets to run, the first loop where it iterates to find a job doesn't come up with anything because job->shouldStop is true
So far this seems fine, the job never runs. I don't see that it ever gets removed from the jobs array but perhaps that's not so bad.
The struggle comes when someone calls ThreadPool::waitForJobToFinish waiting for the aborted job to finish, potentially waiting forever. jobFinishedSignal is never signalled so it hangs.
I don't see a particularly easy way around this...or at least one I can guarantee is less racy than what the code currently does. Any ideas, or at least confirmation that I've found a hole?