andrew4582

SingleEntryGate

Jul 15th, 2011
269
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.67 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3.  
  4. namespace Core.Threading {
  5.  
  6.     /// <summary>
  7.     /// Used to synchronize access to a single-use consumable resource
  8.     /// </summary>
  9.     public sealed class SingleEntryGate {
  10.  
  11.         private const int NOT_ENTERED = 0;
  12.         private const int ENTERED = 1;
  13.  
  14.         private int _status;
  15.  
  16.         /// <summary>
  17.         /// Returns true if this is the first call to TryEnter(), false otherwise
  18.         /// </summary>
  19.         public bool TryEnter() {
  20.            
  21.             int oldStatus = Interlocked.Exchange(ref _status, ENTERED);
  22.            
  23.             return (oldStatus == NOT_ENTERED);
  24.         }
  25.  
  26.     }
  27. }
Advertisement
Add Comment
Please, Sign In to add comment