为什么 volatile 变量通常是私有的?

发布于 2024-10-15 03:58:50 字数 788 浏览 8 评论 0原文

我有两个类 - 类“MyDerived”派生自抽象基类“MyBase”。此外,PrimaryDerivedClass 也继承了MyBase。 我首先创建 PrimaryDerivedClass 的实例,然后创建“MyDerived”的实例。

MyBase 有一个名为 ProcessThread 的受保护方法,当 PrimaryDerivedClass 的新实例时,它的使用方式类似于 ThreadPool.QueueUserWorkItem(ProcessThread) > 并创建了 MyDerived。现在,当我想在某些情况下停止所有线程时,我将一些布尔 易失性变量设置为 TRUE,并且当任何其他线程找到易失性变量的值 = TRUE 时,它会停止处理。

当我在 MyBase 中声明“private volatile bool stopThreads”时,我没有得到预期的行为。然而,当我将其设为staticpublic时,我可以看到我想要实现的目标。 如果私有 volatility 会出现什么问题?

抛开上述要求,我还有一个问题: 通常,我看到 volatile 变量被声明为私有。是否有任何重要的方面导致它通常被声明为私有?

(我是多线程的新手。)

I have two classes - Class 'MyDerived' derives from the abstract base class 'MyBase'. Also PrimaryDerivedClass also inherits MyBase.
I first create the instance of PrimaryDerivedClass which then creates instances of 'MyDerived'.

MyBase has a Protected method called ProcessThread which is used like ThreadPool.QueueUserWorkItem(ProcessThread) when new instance of the PrimaryDerivedClass and MyDerived is created. Now when i want to stop all the threads in some scenario, I am setting some boolean volatile variable to TRUE and when any other thread find volatile variable's value = TRUE, it stops processing.

When I declared 'private volatile bool stopThreads' in MyBase, I didn't get the intended behavior. However, when I made it static or public I could see what I am trying to acheieve.
What would be wrong in case of private volatile?

Keeping the above requirement aside, I have a question further:
Usually, I have seen volatile variable is declared private. Is there any important aspect because of which it's usually declared private?

(I am new to multithreading.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

心作怪 2024-10-22 03:58:50

将访问修饰符从 private 更改为 public 不会对易失性字段的行为产生任何影响(至少不会影响易失性)。您还有其他一些问题,在没有看到您的代码的情况下我们无法猜测。

调试多线程很困难,因为代码执行的方式是不确定的 - 您无法准确预测线程何时运行。

事实上,易失性字段是私有的,因为它们被类用于内部同步。它们是其实现的一部分,而不是其接口。

No changing the access modifier from private to public won't have any effect on the behavior of the volatile field (at least not is volatility). You have some other issue, which we can't make a guess on without seeing your code.

Debugging multi-threading is hard since the way the code executes is non-deterministic - you can't predict when precisely your threads will run.

The fact that volatile fields are private is that they are used for synchronization internally by the class. They are part of it's implementation not its interface.

音栖息无 2024-10-22 03:58:50

易失性字段是私有的,因为它们是字段,而不是易失性字段。如果需要公开值,请使用属性,它允许您隐藏实现细节。

在这种情况下,也许您可​​以将 volatile Boolean 更改为 ManualResetEvent,而无需更改任何调用 obj.StopThreads = true 的代码。可能有人会说,名为 Stop() 的方法比属性更好。

Volatile fields are private because they are fields, not volatile. If you need to expose values, use properties, which allow you to hide implementation details.

In this case, maybe you'll change your volatile Boolean into a ManualResetEvent without needing to change any code calling obj.StopThreads = true. It may be argued that a a method named Stop() would be better than a property all together.

终止放荡 2024-10-22 03:58:50

这种“奇怪”的行为仅仅是因为访问修饰符。 Private 意味着该变量只能从定义它的类中访问。所有派生类都无法访问它,因此当您更改基类的 volatile 变量的值时,它的更改仅适用于基类内部工作 。

所有从基类继承的类都应该有自己的 stop-right-now 实现。但是,当您将此变量设置为公共(这意味着没有访问限制,可以从任何地方访问它)或静态(这意味着该值对于所有实例都相同,因为它“属于”不属于实例而是属于类并且可以访问时,一切都会发生变化按类名而不是实例)。如果您希望衍生工具可以访问您的变量,您应该将其标记为受保护。

http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx - 本文展示了访问修饰符的含义。

This 'strange' behavior is merely because of the access modifier. Private means that this variable is accessible only from the class it was defined in. All derived classes don't have access to it so when you change base class's volatile variable's value it changes for base class inner works only.

All classes that inherit from the base should have their own implementation of stop-right-now. But everything changes when you set this variable as public (which means no access restrictions, it's accessible from everywhere) or static (which means the value is the same for all instances because it 'belongs' not to an instance but to class and is accessible by class name not by instance). If you want your variable to be accessible for derivatives you should mark it as protected.

http://msdn.microsoft.com/en-us/library/wxh6fsc7.aspx - this article shows the meanings of access modifiers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文