BlockingQueue.cs 743 B

123456789101112131415161718192021222324252627282930313233
  1. using System;
  2. using System.Threading;
  3. using System.Collections.Generic;
  4. using System.Text;
  5. namespace OpenSim.Framework.Utilities
  6. {
  7. public class BlockingQueue<T>
  8. {
  9. private Queue<T> _queue = new Queue<T>();
  10. private object _queueSync = new object();
  11. public void Enqueue(T value)
  12. {
  13. lock (_queueSync)
  14. {
  15. _queue.Enqueue(value);
  16. Monitor.Pulse(_queueSync);
  17. }
  18. }
  19. public T Dequeue()
  20. {
  21. lock (_queueSync)
  22. {
  23. if (_queue.Count < 1)
  24. Monitor.Wait(_queueSync);
  25. return _queue.Dequeue();
  26. }
  27. }
  28. }
  29. }