NoahMt4跟单系统制作第十篇锁篇

coding

using System;

using System.Collections.Concurrent;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Threading;

using System.Threading.Tasks;

namespace Copier.Core

{

public sealed class KeyLocker<T> : IDisposable

{

private static readonly object _lockerDictionary = new object();

private static readonly Dictionary<T, LockerObject> _lockerObjects = new Dictionary<T, LockerObject>();

private T _key;

public KeyLocker(T key)

{

_key = key;

LockerObject lockerObject;

lock (_lockerDictionary)

{

if (!_lockerObjects.TryGetValue(_key, out lockerObject))

{

lockerObject = new LockerObject();

_lockerObjects.Add(_key, lockerObject);

}

lockerObject.Count++;

}

Monitor.Enter(lockerObject);

}

public void Dispose()

{

lock (_lockerDictionary)

{

LockerObject lockerObject = _lockerObjects[_key];

lockerObject.Count--;

if (lockerObject.Count == 0)

_lockerObjects.Remove(_key);

Monitor.Exit(lockerObject);

}

}

private sealed class LockerObject

{

public int Count;

}

}

}

NoahWork

以上是 NoahMt4跟单系统制作第十篇锁篇 的全部内容, 来源链接: utcz.com/z/509496.html

回到顶部