I've got thread executing commands from list
do
{
commandExec->criticalSection.EnterCS();
if (!commandExec->commands.empty())
{
commandExec->ExecuteCommand(commandExec->commands.front());
commandExec->commands.pop_front();
}
else
commandExec->criticalSection.SuspendThread();
commandExec->criticalSection.LeaveCS();
} while (commandExec->maintainCommandExecution);
and second thread which adds commands to list:
criticalSection.EnterCS();
commands.push_back(Command(code, parameters));
criticalSection.LeaveCS();
criticalSection.ResumeThread();
First thread could crash while executing command, therefore critical section could not be accessed by second thread:
If a thread terminates while it has ownership of a critical section, the state of the critical section is undefined. Source
So, what's the good way to handle this problem? I could think of some solutions, but they seems tricky (adding third thread, second critical section, etc.)
(criticalSection it's just simple wrapper for CRITICAL_SECTION)