共享锁using范围的实现方法
更新时间:2013年04月04日 22:18:35 作者:
共享锁using范围的实现方法,需要的朋友可以参考一下
复制代码 代码如下:
/// <summary>共享锁</summary>
public class ShareLock
{
ReaderWriterLock Lock;
IDisposable readLock, writeLock;
public IDisposable ReadLock { get { Lock.AcquireReaderLock(-1); return readLock; } }
public IDisposable WriteLock { get { Lock.AcquireWriterLock(-1); return writeLock; } }
public IDisposable UpgradeLock { get { return new UpgraderLocked(Lock); } }
/// <summary>释放所有锁</summary>
public void ReleaseLock() { Lock.ReleaseLock(); }
public ShareLock()
{
this.Lock = new ReaderWriterLock();
this.readLock = new Locked(Lock.ReleaseReaderLock);
this.writeLock = new Locked(Lock.ReleaseWriterLock);
}
class Locked : IDisposable
{
ThreadStart ReleaseLock;
public Locked(ThreadStart ReleaseLock) { this.ReleaseLock = ReleaseLock; }
public void Dispose() { this.ReleaseLock(); }
}
class UpgraderLocked : IDisposable
{
delegate void ReleaseLockHandler(ref LockCookie lockCookie);
ReleaseLockHandler ReleaseLock;
LockCookie Cookie;
public UpgraderLocked(ReaderWriterLock locker)
{
Cookie = locker.UpgradeToWriterLock(-1);
this.ReleaseLock = locker.DowngradeFromWriterLock;
}
public void Dispose() { this.ReleaseLock(ref Cookie); }
}
}
相关文章
基于Kubernetes实现前后端应用的金丝雀发布(两种方案)
这篇文章主要介绍了基于Kubernetes实现前后端应用的金丝雀发布,文中给大家提到了两种常用方案,通过示例代码给大家介绍的非常详细,需要的朋友可以参考下2021-12-12
最新评论