作者:mobiledu2502923977 | 来源:互联网 | 2023-07-21 21:22
Ivegotthreadexecutingcommandsfromlist我从列表中获得了执行命令的线程do{commandExec->criticalSecti
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)
(criticalSection它只是CRITICAL_SECTION的简单包装器)
2 个解决方案