C#Locking,Properties&权限

时间:2009-09-29 01:24:20

标签: c# properties locking modifiers

当需要多线程访问时,我一直在使用值类型属性的锁定。此外,我一直意味着要更加努力地应用适当的访问修饰符,尤其是在我的库代码中,这些代码开始在多个项目中变得有用。我编写了一些代码,并希望对其中的各种策略请求注释以获取属性并锁定它们包含的成员变量。感谢。

using System;

public class Program
{
    static void Main(string[] args)
    {
        SomeValueType svt = new SomeValueType();
        SomeReferenceType srt = new SomeReferenceType();        
        PermissionsAndLocking p = new PermissionsAndLocking(5, svt, srt);

        //Invalid.
        //p.X = 6;

        //Invalid
        //p.Svt = new SomeValueType();
        //Invalid
        //p.Svt.X = 1;
        //Valid, but changes a copy of p.Svt because Svt is a value type.
        SomeValueType svt2 = p.Svt;
        svt2.X = 7;

        //Invalid
        //p.Srt = new SomeReferenceType();
        //Valid, change the member data of p.Srt.
        p.Srt.X = 8;        
        SomeReferenceType srt2 = p.Srt;
        srt2.X = 9;

        Console.WriteLine("Press the any key.");
        Console.Read();
    }
}

public class PermissionsAndLocking
{
    //_x cannot be changed outside the class.
    //_x cannot be changed "at the same time" it is being accessed???
    private readonly object _xLock = new object();
    private int _x;
    public int X
    {
        get
        {
            lock (_xLock)
            {
                return _x;
            }
        }
        private set
        {
            lock (_xLock)
            {
                _x = value;
            }
        }
    }

    //_svt and its members cannot be assigned to outside the class.
    //_svt cannot be changed "at the same time as" it is being accessed.
    private readonly object _svtLock = new object();
    private SomeValueType _svt;
    public SomeValueType Svt
    {
        get
        {
            lock (_svtLock)
            {
                return _svt;
            }
        }
        private set
        {
            lock (_svtLock)
            {
                _svt = value;
            }
        }
    }

    //private on set works for = but member data can still be manipulated...
    //Locking isn't complete because the reference is returned and can be accessed at a later time???
    private readonly object _srtLock = new object();
    private SomeReferenceType _srt;
    public SomeReferenceType Srt
    {
        get
        {
            lock (_srtLock)
            {
                return _srt;
            }
        }
        private set
        {
            lock (_srtLock)
            {
                _srt = value;
            }
        }
    }

    public PermissionsAndLocking(int x, SomeValueType svt, SomeReferenceType srt)
    {
        _x = x;
        _svt = svt;
        _srt = srt;
    }
}

public struct SomeValueType
{
    public int X;
}

public class SomeReferenceType
{
    public int X;
}

2 个答案:

答案 0 :(得分:2)

您需要阅读有关多线程和并发的信息。锁定是关于保护不变量,同时它们是无效的,即,当不变量无效时,阻止对不变量所依赖的共享存储器的并发访问。第一步是了解代码例程的不变量,其次,在 代码块是不变的无效。

例如,属性getter没有内在需要与锁同步。它只读取属性值。这个读取正在进行时,什么是不变量无效?读取变量,递增变量,然后将递增的值写回属性的操作可能需要锁定,但锁定单个getter和setter将完全不合适。整个操作,包括读取和写入,都必须在受保护的块内。

答案 1 :(得分:-3)

  1. 您应始终lock一个静态对象,因此您应将_svtLock标记为静态,以使锁具有效果。
  2. _x不能在课外改变。真正。必须通过X更改。
  3. 如果您正确实施锁定(请参阅1),则在访问时无法更改_x