}
class FastLock : IDisposable { private const int CONST_LOCK = 1; private const int CONST_UNLOCK = 0; SpinWait spinner = new SpinWait(); private int _isLock = CONST_UNLOCK; public FastLock() { } public FastLock Lock() { sylvania bulb guide Helper_LockSection(); return this; sylvania bulb guide sylvania bulb guide } sylvania bulb guide private void Helper_LockSection() { if (Interlocked.CompareExchange(ref _isLock, CONST_LOCK, CONST_UNLOCK) != CONST_UNLOCK) { do { Thread.Sleep(1); //spinner.SpinOnce(); } while (Interlocked.CompareExchange(ref _isLock, CONST_LOCK, CONST_UNLOCK) != CONST_UNLOCK); } } private void Helper_UnlockSection() { sylvania bulb guide sylvania bulb guide if (Interlocked.CompareExchange(ref _isLock, CONST_UNLOCK, CONST_LOCK) != CONST_LOCK) { throw new Exception("This cant happend"); //var spinner = new SpinWait(); //do //{ sylvania bulb guide sylvania bulb guide // spinner.SpinOnce(); sylvania bulb guide sylvania bulb guide //} //while (Interlocked.CompareExchange(ref _isLock, CONST_UNLOCK, CONST_LOCK) != CONST_LOCK); sylvania bulb guide } } sylvania bulb guide public void Dispose() { sylvania bulb guide Helper_UnlockSection(); } }
▼ 2014 (3) ▼ červen (1) C# Fast Lock Implementation If you lock only a sma... ► květen (1) ► únor (1) ► 2013 (2) ► srpen (1) ► červenec (1)
No comments:
Post a Comment