C#锁定,属性和权限

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

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;
}

您需要阅读有关多线程和并发性的内容。 锁定是为了在不变量无效的情况下保护不变量,即当不变量无效时,防止不变量依赖的共享存储器的并发访问。 第一步是了解你的代码例程具有什么不变性,其次,在哪个代码块中是不变的无效。

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


  • 您应该始终lock一个静态对象,因此您应该将_svtLock标记为静态,以便使锁具有效果。
  • _x不能在课堂外进行更改。 真正。 它必须通过X进行更改。
  • 如果您正确执行锁定(请参阅1),则_x在访问时无法更改。
  • 链接地址: http://www.djcxy.com/p/57923.html

    上一篇: C# Locking, Properties & Permissions

    下一篇: MongoDb Compound Indexes: Performance Based on Order of Keys