我使用SDL2的线程库编写了一个线程池,该库使用以下线程函数:
int threadFunc(void * pData)
{
ThreadData* data = (ThreadData*)pData;
SDLTaskManager* pool = data->pool;
Task* task = nullptr;
while (true)
{
SDL_LockMutex(pool->getMutex());
while (!pool->isRunning() && !pool->hasTasks())
{
SDL_CondWait(pool->getCondition(), pool->getMutex());
}
if (pool->shuttingDown())
{
return 0;
}
task = pool->getNextTask(); //SDL_CondWait relocks mutex, so this *should* be thread safe.
if (task != nullptr)
{
pool->notifyThreadWorking(true);
data->taskCount++;
}
else
{
pool->stop();
SDL_UnlockMutex(pool->getMutex());
continue;
}
SDL_UnlockMutex(pool->getMutex());
task->execute();
SDL_LockMutex(pool->getMutex());
pool->notifyThreadWorking(false);
pool->addCompleteTask(task);
SDL_UnlockMutex(pool->getMutex());
}
return 0;
}
当调用pool-> getNextTask()时,我得到调试断言错误“Deque iterator is dereferencable”。我已经通过调用堆栈检查了池维护的deque的内容,发现调用此函数时它肯定不是空的。
功能代码如下:
Task * SDLTaskManager::getNextTask()
{
Task* t = nullptr;
if (!mCurrentTasks.empty())
{
t = mCurrentTasks.front(); //this part is causing the error
mCurrentTasks.pop_front(); //this is a std::deque<Task*>
}
return t;
}
deque中包含有效数据,所以我对导致问题的原因感到茫然。