ThreadPoolJob Memory Management


#1

Hello Julian, let’s say I have 3 threads assigned to a ThreadPool and I add 10 jobs to the Pool. 3 are now active and can return

ThreadPoolJob::jobHasFinishedAndShouldBeDeleted

The other 7 however are inactive and removeAllJobs will just remove them from the Array without deletion.

Is there any chance you could expand removeAllJobs so that…

bool removeAllJobs (const bool interruptRunningJobs,
const int timeOutMilliseconds,bool deleteInactiveJobs=false);

[code]bool ThreadPool::removeAllJobs (const bool interruptRunningJobs,
const int timeOutMs,bool deleteInactiveJobs)
{
lock.enter();

for (int i = jobs.size(); --i >= 0;)
{
    ThreadPoolJob* const job = (ThreadPoolJob*) jobs.getUnchecked(i);

    if (job->isActive)
    {
        if (interruptRunningJobs)
            job->signalJobShouldExit();
    }
    else
    {
        jobs.remove (i);
		if (deleteInactiveJobs)
			delete job;
    }
}

lock.exit();

const uint32 start = Time::getMillisecondCounter();

while (jobs.size() > 0)
{
    if (timeOutMs >= 0 && Time::getMillisecondCounter() >= start + timeOutMs)
        return false;

    Thread::sleep (2);
}

return true;

}
[/code]

That way we can just call removeAllJobs(true,4000,true); in our Destructor and know the jobs are going to be deleted properly. (Or give me access to the lock!)

Tnx

Justin


#2

Yes - that does seem like a really sensible idea, thanks!