【发布时间】:2012-03-06 14:03:58
【问题描述】:
我有两个线程:'main' 和 'worker',以及一个全局变量 bool isQuitRequested,main 线程将使用它来通知 worker,何时该退出其 while 循环(类似于这个:while(isQuitRequested == false) { ... do some stuff ... })
现在,我有点担心...
考虑到只有一个线程 (main) 执行 isQuitRequested = true 操作,而另一个 (worker) 只执行检查而不执行其他操作,我是否需要对 isQuitRequested 使用某种互斥锁保护?
我已阅读What could happen if two threads access the same bool variable at the same time?。 我的情况类似,但情况不一样...
【问题讨论】:
标签: c++ multithreading synchronization shared-state