ThreadPoolJob deletion: theading issue, maybe?

Is the following sequence a crash risk in ThreadPool?

  • Add a job with addJob(new Job(), false) - we will try and delete it later
  • Job completes and calls something like triggerAsyncUpdate
  • Now in our handleAsyncCallback we have no way of knowing whether the addToDelete list function is complete (see below) so there’s a race condition with a crash if we delete the job before “if (job->shouldBeDeleted)” executes?

Possibly I can avoid this by calling removeJob even though the job will no longer be in the jobs list as it’ll force it through one of the code regions protected by ThreadPool’s internal lock.

What am I missing, is calling addJob(Job *, false) actually broken or am I doing it wrong, how do I know I can delete my job?

void ThreadPool::addToDeleteList (OwnedArray<ThreadPoolJob>& deletionList, ThreadPoolJob* job) const
{
    job->shouldStop = true;
    job->pool = nullptr;

    if (job->shouldBeDeleted)
        deletionList.add (job);
}