リストからコマンドを実行するスレッドがあります
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);
リストにコマンドを追加する2番目のスレッド:
criticalSection.EnterCS();
commands.push_back(Command(code, parameters));
criticalSection.LeaveCS();
criticalSection.ResumeThread();
コマンドの実行中に最初のスレッドがクラッシュする可能性があるため、2番目のスレッドがクリティカルセクションにアクセスできませんでした。
スレッドがクリティカルセクションの所有権を持っている間に終了した場合、クリティカルセクションの状態は未定義です。 ソース
それで、この問題を処理するための良い方法は何ですか? 私はいくつかの解決策を考えることができましたが、それらはトリッキーなようです(3番目のスレッド、2番目のクリティカルセクションなどを追加する)
(criticalSectionは、CRITICAL_SECTIONの単純なラッパーです)