C# 中的线程安全属性 [英] Thread Safe Properties in C#

查看:30
本文介绍了C# 中的线程安全属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在 C# 中创建线程安全属性,并且我想确保我在正确的路径上 - 这就是我所做的 -

I am trying to create thread safe properties in C# and I want to make sure that I am on the correct path - here is what I have done -

private readonly object AvgBuyPriceLocker = new object();
private double _AvgBuyPrice;
private double AvgBuyPrice 
{
    get
    {
        lock (AvgBuyPriceLocker)
        {
            return _AvgBuyPrice;
        }
    }
    set
    {
        lock (AvgBuyPriceLocker)
        {
            _AvgBuyPrice = value;
        }
    }
}

阅读这篇文章,似乎这不是正确的做法 -

Reading this posting, it would seem as if this isn't the correct way of doing it -

使用 get/set 的 C# 线程安全

然而,这篇文章似乎另有暗示,

however, this article seems to suggest otherwise,

http://www.codeproject.com/KB/cs/Synchronized.aspx

有没有人有更明确的答案?

Does anybody have a more definitive answer?

我想为这个属性做 Getter/Setter 的原因是 b/c 我实际上希望它在设置时触发一个事件 - 所以代码实际上是这样的 -

The reason that I want to do the Getter/Setter for this property is b/c I actually want it to fire an event when it is set - so the code would actually be like this -

public class PLTracker
{

    public PLEvents Events;

    private readonly object AvgBuyPriceLocker = new object();
    private double _AvgBuyPrice;
    private double AvgBuyPrice 
    {
        get
        {
            lock (AvgBuyPriceLocker)
            {
                return _AvgBuyPrice;
            }
        }
        set
        {
            lock (AvgBuyPriceLocker)
            {
                Events.AvgBuyPriceUpdate(value);
                _AvgBuyPrice = value;
            }
        }
    }
}

public class PLEvents
{
    public delegate void PLUpdateHandler(double Update);
    public event PLUpdateHandler AvgBuyPriceUpdateListener;

    public void AvgBuyPriceUpdate(double AvgBuyPrice)
    {
        lock (this)
        {
            try
            {
                if (AvgBuyPriceUpdateListener!= null)
                {
                    AvgBuyPriceUpdateListener(AvgBuyPrice);
                }
                else
                {
                    throw new Exception("AvgBuyPriceUpdateListener is null");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
            }
        }
    }
}

我对让我的代码线程安全感到很陌生,所以请随时告诉我我是否以完全错误的方式进行操作!

I am pretty new to making my code thread safe so please feel free to tell me if I am going about it in the totally wrong way!

推荐答案

由于您拥有原始值,因此此锁定可以正常工作 - 另一个问题中的问题是属性值是一个更复杂的类(可变引用类型) - 锁定将保护访问和检索您的类持有的 double 值的实例.

Since you have a primitive value this locking will work fine - the issue in the other question was that the property value was a more complex class (a mutable reference type) - the locking will protect accessing and retrieving the instance of the double value held by your class.

如果您的属性值是可变引用类型,另一方面,一旦使用其方法检索到类实例,锁定将无法防止更改类实例,这正是其他发布者希望它做的.

If your property value is a mutable reference type on the other hand locking will not protect from changing the class instance once retrieved using its methods, which is what the other poster wanted it to do.

这篇关于C# 中的线程安全属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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