是volatile bool为线程控制被认为是错误吗? [英] Is volatile bool for thread control considered wrong?

查看:134
本文介绍了是volatile bool为线程控制被认为是错误吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

由于我对此问题的回答,我开始阅读关键字 volatile 以及关于它的共识。我看到有很多关于它的信息,一些老似乎错误现在和很多新的,说它几乎没有地方在多线程编程。因此,我想澄清一个具体的用法(在这里找不到一个确切的答案SO)。

As a result of my answer to this question, I started reading about the keyword volatile and what the consensus is regarding it. I see there is a lot of information about it, some old which seems wrong now and a lot new which says it has almost no place in multi-threaded programming. Hence, I'd like to clarify a specific usage (couldn't find an exact answer here on SO).

我也想指出,我明白要求一般写多线程代码,为什么 volatile 不解决问题。仍然,我看到代码使用 volatile 线程控制在代码库我工作。此外,这是唯一的情况,我使用 volatile 关键字,因为所有其他共享资源都正确同步。

I also want to point out I do understand the requirements for writing multi-threaded code in general and why volatile is not solving things. Still, I see code using volatile for thread control in code bases I work in. Further, this is the only case I use the volatile keyword as all other shared resources are properly synchronized.

假设我们有一个类:

class SomeWorker
{
public:
    SomeWorker() : isRunning_(false) {}
    void start() { isRunning_ = true; /* spawns thread and calls run */ }
    void stop() { isRunning_ = false; }

private:
    void run()
    {
        while (isRunning_)
        {
            // do something
        }
    }
    volatile bool isRunning_;
};

为了简单起见,有些东西被遗漏了,但最重要的是,它在新生成的线程中检查( volatile )布尔值以确定它是否应该停止。

For simplicity some things are left out, but the essential thing is that an object is created which does something in a newly spawned thread checking a (volatile) boolean to know if it should stop. This boolean value is set from another thread whenever it wants the worker to stop.

我的理解是,使用的原因是, volatile 在这种特定情况下是简单地避免任何优化,它将缓存在循环的寄存器。因此,导致无限循环。没有必要正确同步的东西,因为工作线程将最终得到新的值?

My understanding has been that the reason to use volatile in this specific case is simply to avoid any optimization which would cache it in a register for the loop. Hence, resulting in an infinite loop. There is no need to properly synchronize things, because the worker thread will eventually get the new value?

我想了解,如果这被认为完全错误如果正确的方法是使用同步变量?编译器/体系结构/核心之间有区别吗?也许这只是一个马虎的方法值得避免?

I'd like to understand if this is considered completely wrong and if the right approach is to use a synchronized variable? Is there a difference between compiler/architecture/cores? Maybe it's just a sloppy approach worth avoiding?

如果有人澄清这一点,我会很高兴。非常感谢!

I'd be happy if someone would clarify this. Thanks!

EDIT

)如何选择解决此问题。

I'd be interested to see (in code) how you choose to solve this.

推荐答案

volatile 用于此类目的。 :这是标准C ++的扩展 Microsoft

volatile can be used for such purposes. However this is an extension to standard C++ by Microsoft:


Microsoft专用



被声明为volatile的对象是(...)

Microsoft Specific

Objects declared as volatile are (...)


  • 写入volatile对象(volatile write) (...)

  • 读取volatile对象(volatile读取)具有Acquire语义; (...)

这允许将挥发性对象用于多线程应用程序中的内存锁定和发布。 > (emph。added)

This allows volatile objects to be used for memory locks and releases in multithreaded applications.(emph. added)

就我所知, C ++编译器, volatile bool 是最实用的目的a 原子

That is, as far as I understand, when you use the Visual C++ compiler, a volatile bool is for most practical purposes an atomic<bool>.

请注意,较新的 VS版本会添加 / volatile开关控制此行为,因此这仅在 / volatile:ms 已激活。

It should be noted that newer VS versions add a /volatile switch that controls this behavior, so this only holds if /volatile:ms is active.

这篇关于是volatile bool为线程控制被认为是错误吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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