source:
binary-improvements/7dtd-server-fixes/src/BlockingQueue.cs@
343
Last change on this file since 343 was 325, checked in by , 6 years ago | |
---|---|
File size: 617 bytes |
Rev | Line | |
---|---|---|
[182] | 1 | using System.Collections.Generic; |
2 | using System.Threading; | |
3 | ||
[325] | 4 | namespace AllocsFixes { |
5 | public class BlockingQueue<T> { | |
6 | private readonly Queue<T> queue = new Queue<T> (); | |
7 | private bool closing; | |
[182] | 8 | |
[325] | 9 | public void Enqueue (T item) { |
[182] | 10 | lock (queue) { |
11 | queue.Enqueue (item); | |
12 | Monitor.PulseAll (queue); | |
13 | } | |
14 | } | |
15 | ||
[325] | 16 | public T Dequeue () { |
[182] | 17 | lock (queue) { |
18 | while (queue.Count == 0) { | |
[190] | 19 | if (closing) { |
[325] | 20 | return default (T); |
[190] | 21 | } |
[325] | 22 | |
[182] | 23 | Monitor.Wait (queue); |
24 | } | |
[325] | 25 | |
[182] | 26 | return queue.Dequeue (); |
27 | } | |
28 | } | |
29 | ||
[325] | 30 | public void Close () { |
[190] | 31 | lock (queue) { |
32 | closing = true; | |
33 | Monitor.PulseAll (queue); | |
34 | } | |
35 | } | |
[182] | 36 | } |
[325] | 37 | } |
Note:
See TracBrowser
for help on using the repository browser.