m1o2

CallOnce My slution

Mar 8th, 2014
186
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  public class OnceFlag
  2.  {
  3.         private const int NotCalled = 0;
  4.         private const int Called = 1;
  5.         private int _state = NotCalled;
  6.  
  7.         public ManualResetEvent _resetHanlde = new ManualResetEvent(false);
  8.  
  9.         internal void SetHandle()
  10.         {
  11.             _resetHanlde.Set();
  12.         }
  13.  
  14.         internal void WaitHandle()
  15.         {
  16.             _resetHanlde.WaitOne();
  17.         }
  18.  
  19.         internal void ResetHandle()
  20.         {
  21.             _resetHanlde.Reset();
  22.         }
  23.  
  24.         internal bool CheckIfNotCalledAndSet
  25.         {
  26.             get
  27.             {
  28.                 var prev = Interlocked.Exchange(ref _state, Called);
  29.  
  30.                 return prev == NotCalled;
  31.             }
  32.         }
  33.  
  34.         internal void Reset()
  35.         {
  36.             Interlocked.Exchange(ref _state, NotCalled);
  37.             _resetHanlde.Reset();
  38.         }
  39.  
  40.  }
  41.  
  42.  public static class Call
  43.  {   public static void Once(OnceFlag flag, Action action)
  44.         {
  45.             if (flag.CheckIfNotCalledAndSet)
  46.             {
  47.                 action.Invoke();
  48.  
  49.                 flag.SetHandle();
  50.             }
  51.  
  52.             flag.WaitHandle();
  53.         }
  54.  }
  55.  
  56. /***********************************
  57. Testing the performance:
  58. **////////////////////////////////
  59. class Program
  60. {
  61.         static OnceFlagSimple _drorFlag = new OnceFlagSimple();
  62.         static OnceFlag _mineFlag = new OnceFlag();
  63.  
  64.         static void Main(string[] args)
  65.         {
  66.             var watch = new Stopwatch();
  67.  
  68.             watch.Start();
  69.             DrorTest();
  70.             watch.Stop();
  71.  
  72.             Console.WriteLine("It took to Dror: " + watch.Elapsed);
  73.  
  74.             watch.Reset();
  75.  
  76.             watch.Start();
  77.             MineTest();
  78.             watch.Stop();
  79.  
  80.             Console.WriteLine("It took to me  : " + watch.Elapsed);
  81.         }
  82.  
  83.         static void MineTest()
  84.         {
  85.             var t1 = new Thread(() => MineDoOnce(1));
  86.             var t2 = new Thread(() => MineDoOnce(2));
  87.             var t3 = new Thread(() => MineDoOnce(3));
  88.             var t4 = new Thread(() => MineDoOnce(4));
  89.  
  90.             t1.Start();
  91.             t2.Start();
  92.             t3.Start();
  93.             t4.Start();
  94.  
  95.             t1.Join();
  96.             t2.Join();
  97.             t3.Join();
  98.             t4.Join();
  99.         }
  100.  
  101.         static void MineDoOnce(int index)
  102.         {
  103.             Call.Once(_mineFlag, () => Console.WriteLine("Callled (" + index + ")"));
  104.         }
  105.  
  106.         static void DrorTest()
  107.         {
  108.             var t1 = new Thread(() => DrorDoOnce(1));
  109.             var t2 = new Thread(() => DrorDoOnce(2));
  110.             var t3 = new Thread(() => DrorDoOnce(3));
  111.             var t4 = new Thread(() => DrorDoOnce(4));
  112.  
  113.             t1.Start();
  114.             t2.Start();
  115.             t3.Start();
  116.             t4.Start();
  117.  
  118.             t1.Join();
  119.             t2.Join();
  120.             t3.Join();
  121.             t4.Join();
  122.  
  123.         }
  124.  
  125.         static void DrorDoOnce( int index)
  126.         {
  127.             Call.Once(_drorFlag, () => Console.WriteLine("Callled (" + index + ")"));
  128.         }
  129. }
  130.  
  131. /***************************************
  132. Results:
  133. Callled (1)
  134. It took to Dror: 00:00:00.0034065
  135. Callled (1)
  136. It took to me  : 00:00:00.0018291
  137. Press any key to continue . . .
  138. **//////////////////////////////////
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×