Advertisement
Guest User

Untitled

a guest
Jul 29th, 2017
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.34 KB | None | 0 0
  1. using System;
  2. using System.Threading;
  3. using System.Collections;
  4. using System.Collections.Generic;
  5.  
  6. namespace Uber.Collections
  7. {
  8.     /// <summary>
  9.     /// Implements thread-safe access for Enumerating through a collection of items
  10.     /// </summary>
  11.     /// <typeparam name="T">Items</typeparam>
  12.     ///
  13.     public class SafeEnumerator<T> : IEnumerator<T>
  14.     {
  15.         private readonly IEnumerator<T> m_Inner;
  16.         private readonly ReaderWriterLockSlim m_Lock;
  17.  
  18.         public SafeEnumerator(IEnumerator<T> inner, ReaderWriterLockSlim @lock)
  19.         {
  20.             m_Inner = inner;
  21.             m_Lock = @lock;
  22.  
  23.             m_Lock.EnterReadLock();
  24.         }
  25.  
  26.         /// <summary>
  27.         /// Implementation of IDisposable
  28.         /// </summary>
  29.         public void Dispose()
  30.         {
  31.             m_Lock.ExitReadLock();
  32.         }
  33.  
  34.         /// <summary>
  35.         /// Implementation of the Enumeration
  36.         /// </summary>
  37.         /// <returns></returns>
  38.  
  39.         public bool MoveNext()
  40.         {
  41.             return m_Inner.MoveNext();
  42.         }
  43.  
  44.         public void Reset()
  45.         {
  46.             m_Inner.Reset();
  47.         }
  48.  
  49.         public T Current
  50.         {
  51.             get { return m_Inner.Current; }
  52.         }
  53.  
  54.         object IEnumerator.Current
  55.         {
  56.             get { return Current; }
  57.         }
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement