TaskList - wait_until_not_empty, . , (1000) ( - ) . TaskList, auto- reset, , . pop/push, :
WaitableTaskList::WaitableTaskList()
{
non_empty_event = CreateEvent(NULL, FALSE, FALSE, NULL);
}
Task* WaitableTaskList::wait_and_pop_front(DWORD timeout)
{
WaitForSingleObject(non_empty_event, timeout);
Task* result = task_list.pop_front();
if (!task_list.empty())
SetEvent(non_empty_event);
return result;
}
void WaitableTaskList::push_back(Task* item)
{
task_list.push_back(item);
SetEvent(non_empty_event);
}
, wait_and_pop_front().
EDIT: This is actually a bad solution. There is a way for un_empty_event to be raised, even if the list is empty. The situation requires 2 threads trying a pop list, and the list contains 2 elements. If the list becomes empty between if and SetEvent, we will have an incorrect state. Obviously, we also need to implement synchronization. At this moment, I will again review the simple Dream :-)
source
share