我希望我的线程更优雅地关闭,所以我尝试实现一个简单的信号机制.我不认为我想要一个完全事件驱动的线程,所以我有一个工作人员可以使用关键部分 Monitor 优雅地停止它(相当于 C# lock我相信):
绘图线程.h
class DrawingThread { bool stopRequested; Runtime::Monitor CSMonitor; CPInfo *pPInfo; //More.. }
绘图线程.cpp
void DrawingThread::Run() { if (!stopRequested) //Time consuming call#1 if (!stopRequested) { CSMonitor.Enter(); pPInfo = new CPInfo(/**/); //Not time consuming but pPInfo must either be null or constructed. CSMonitor.Exit(); } if (!stopRequested) { pPInfo->foobar(/**/);//Time consuming and can be signalled } if (!stopRequested) { //One more optional but time consuming call. } } void DrawingThread::RequestStop() { CSMonitor.Enter(); stopRequested = true; if (pPInfo) pPInfo->RequestStop(); CSMonitor.Exit(); }
我了解(至少在 Windows 中)Monitor/lock 是最便宜的线程同步原语,但我希望避免过度使用.我应该包装这个布尔标志的每次读取吗?它被初始化为 false,并且仅在请求停止时设置一次为 true(如果在任务完成之前请求它).
我的导师建议保护甚至 bool 的,因为读/写可能不是原子的.我觉得这个一击旗是证明规则的例外?