它是确定读取共享布尔标志没有锁定它时,另一个线程可以设置(最多一次)? [英] Is it ok to read a shared boolean flag without locking it when another thread may set it (at most once)?

查看:128
本文介绍了它是确定读取共享布尔标志没有锁定它时,另一个线程可以设置(最多一次)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我的线程更从容的关闭,所以我想实现一个简单的信号机制。我不认为我想要一个完全事件驱动的线程,所以我有一个工人,一个方法来graceully使用临界区显示器(相当于C#<$停止C $ C>锁定我相信):

I would like my thread to shut down more gracefully so I am trying to implement a simple signalling mechanism. I don't think I want a fully event-driven thread so I have a worker with a method to graceully stop it using a critical section Monitor (equivalent to a C# lock I believe):

DrawingThread.h

DrawingThread.h

class DrawingThread {
    bool stopRequested;
    Runtime::Monitor CSMonitor;
    CPInfo *pPInfo;
    //More..
}

DrawingThread.cpp

DrawingThread.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中)显示器 / 锁定是最便宜的线程同步原语,但我很希望避免用眼过度。我应该环绕此布尔标志的每一次读?它被初始化为假,只有当被要求停止一次设置为true(如果在任务完成之前,请求)。

I understand (at least in Windows) Monitor/locks are the least expensive thread synchronization primitive but I am keen to avoid overuse. Should I be wrapping each read of this boolean flag? It is initialized to false and only set once to true when stop is requested (if it is requested before the task completes).

我的导师建议,以保护甚至布尔是因为读/写可能不是原子。我觉得这一个镜头标志,证明规则的例外呢?

My tutors advised to protect even bool's because read/writing may not be atomic. I think this one shot flag is the exception that proves the rule?

推荐答案

这是从来没有确定读的东西在不同的线程可能被修改不同步。什么级别的同步,需要取决于你实际上是读什么。对于基本类型,你应该看看原子读取,例如形式为的std ::原子&LT;布尔方式&gt;

It is never OK to read something possibly modified in a different thread without synchronization. What level of synchronization is needed depends on what you are actually reading. For primitive types, you should have a look at atomic reads, e.g. in the form of std::atomic<bool>.

同步总是需要的原因是,处理器将有可能在一个高速缓存线的共享数据。它没有理由,如果没有同步,以更新该值以在不同的线程可能改变的值。更糟的是,是,如果没有同步,可能写的东西,如果存储接近值被改变和同步的错误值。

The reason synchronization is always needed is that the processors will have the data possibly shared in a cache line. It has no reason to update this value to a value possibly changed in a different thread if there is no synchronization. Worse, yet, if there is no synchronization it may write the wrong value if something stored close to the value is changed and synchronized.

这篇关于它是确定读取共享布尔标志没有锁定它时,另一个线程可以设置(最多一次)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆