LockWait.cs 928 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. using System;
  2. using System.Threading;
  3. namespace IFramework.Net
  4. {
  5. /// <summary>
  6. /// 自旋锁
  7. /// </summary>
  8. public sealed class LockWait : IDisposable
  9. {
  10. private LockParam _param = null;
  11. /// <summary>
  12. /// Ctor
  13. /// </summary>
  14. /// <param name="param"></param>
  15. public LockWait(ref LockParam param)
  16. {
  17. this._param = param;
  18. while (Interlocked.CompareExchange(ref param.signal, 1, 0) == 1)
  19. {
  20. Thread.Sleep(param.sleepInterval);
  21. }
  22. }
  23. /// <summary>
  24. /// 释放
  25. /// </summary>
  26. public void Dispose()
  27. {
  28. Interlocked.Exchange(ref _param.signal, 0);
  29. }
  30. }
  31. /// <summary>
  32. /// 自旋锁信号
  33. /// </summary>
  34. public class LockParam
  35. {
  36. internal int signal = 0;
  37. internal int sleepInterval = 1;
  38. }
  39. }