Example #1
0
void TaskQueue::pollProcessedTasks(TimeFrame timeFrame)
{
	TaskUnitQueue processedCopy;
	{
		std::unique_lock<std::mutex> l(mProcessedQueueMutex);
		processedCopy = mProcessedTaskUnits;
		if (!mProcessedTaskUnits.empty()) {
			mProcessedTaskUnits = TaskUnitQueue();
		}
	}
	while (!processedCopy.empty()) {

		TaskUnit* taskUnit = processedCopy.front();
		processedCopy.pop();
		try {
			taskUnit->executeInMainThread();
		} catch (const std::exception& ex) {
			S_LOG_FAILURE("Error when executing task in main thread." << ex);
		} catch (...) {
			S_LOG_FAILURE("Unknown error when executing task in main thread.");
		}
		try {
			delete taskUnit;
		} catch (const std::exception& ex) {
			S_LOG_FAILURE("Error when deleting task in main thread." << ex);
		} catch (...) {
			S_LOG_FAILURE("Unknown error when deleting task in main thread.");
		}
		//Try to keep the time spent here each frame down, to keep the framerate up.
		if (!timeFrame.isTimeLeft()) {
			break;
		}
	}
	//If there are any unprocessed tasks, put them back at the front of the queue.
	if (!processedCopy.empty()) {
		std::unique_lock<std::mutex> l(mProcessedQueueMutex);
		TaskUnitQueue queueCopy(mProcessedTaskUnits);
		mProcessedTaskUnits = processedCopy;
		while (!queueCopy.empty()) {
			mProcessedTaskUnits.push(queueCopy.front());
			queueCopy.pop();
		}
	}
}