示例#1
0
void WorkQueue_Impl::worker_main()
{
	while (true)
	{
		int wakeup_reason = Event::wait(stop_event, work_available_event);
		if (wakeup_reason != 1)
			break;
		MutexSection mutex_lock(&mutex);
		if (!queued_items.empty())
		{
			WorkItem *item = queued_items.front();
			queued_items.erase(queued_items.begin());
			mutex_lock.unlock();
			item->process_work();
			mutex_lock.lock();
			finished_items.push_back(item);
			mutex_lock.unlock();
			set_wakeup_event();
		}
		else
		{
			work_available_event.reset();
		}
	}
}
示例#2
0
	void WorkQueue_Impl::worker_main()
	{
		while (true)
		{
			std::unique_lock<std::mutex> mutex_lock(mutex);
			worker_event.wait(mutex_lock, [&]() { return stop_flag || !queued_items.empty(); });

			if (stop_flag)
				break;

			WorkItem *item = queued_items.front();
			queued_items.erase(queued_items.begin());
			mutex_lock.unlock();

			item->process_work();

			mutex_lock.lock();
			finished_items.push_back(item);
			mutex_lock.unlock();
		}
	}