【线程同步学习笔记】C#中的lock关键字

1. lock关键字保证一个代码块在执行的过程中不会受到其他线程的干扰,这是通过在该代码块的运行过程中对特定的对象加互斥锁来实现的。

2. lock关键字的参数必须是引用类型的对象。lock对基本数据类型如int,long等无效,因为它所作用的类型必须是对象。如果传入long类型数据,势必被转换为Int64结构类型,则加锁的是全新的对象引用。如果需要对它们进行互斥访问限制,可以使用System.Threading.Interlocked类提供的方法,这个类是提供原子操作的。

3. lock(this)的使用要慎重。共有类型中使用lock(this),如果新的对象被创建并加锁,极易造成死锁。

4. 锁定ICollection类型对象时,应lock其SyncRoot属性。

SyncRoot属性在接口ICollection中声明,其实现方式各不相同。

例如在Collection(System.Collections.ObjectModel)中实现如下:

object ICollection.SyncRoot
{
          get
          {
                    if (this._syncRoot == null)
                    {
                              ICollection items = this.items as ICollection;
                              if (items != null)
                              {
                                        this._syncRoot = items.SyncRoot;
                              }
                              else
                              {
                                        Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
                              }
                    }
                    return this._syncRoot;
          }
}
而在List<T>,ArrayList等类中实现如下:

object ICollection.SyncRoot
{
          get
          {
                    if (this._syncRoot == null)
                    {
                              Interlocked.CompareExchange(ref this._syncRoot, new object(), null);
                    }
                    return this._syncRoot;
          }
}
 
在Array类中则直接返回了this:

public object SyncRoot
{
          get
          {
                    return this;
          }
}

5. lock关键字是用Monitor(管程)类实现的

lock(x)
{
    DoSomething();
}

System.Object obj = (System.Object)x;
System.Threading.Monitor.Enter(obj);
try
{
    DoSomething();
}
finally
{
    System.Threading.Monitor.Exit(obj);
}

以上两段代码是等效的。(MSDN)

使用lock关键字相对于Monitor类在使用上更简单,也更加保险。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值