Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Threading;
|
---|
4 |
|
---|
5 | namespace AllocsFixes
|
---|
6 | {
|
---|
7 | public class BlockingQueue<T>
|
---|
8 | {
|
---|
9 | private bool closing = false;
|
---|
10 | private Queue<T> queue = new Queue<T> ();
|
---|
11 |
|
---|
12 | public void Enqueue (T item)
|
---|
13 | {
|
---|
14 | lock (queue) {
|
---|
15 | queue.Enqueue (item);
|
---|
16 | Monitor.PulseAll (queue);
|
---|
17 | }
|
---|
18 | }
|
---|
19 |
|
---|
20 | public T Dequeue ()
|
---|
21 | {
|
---|
22 | lock (queue) {
|
---|
23 | while (queue.Count == 0) {
|
---|
24 | if (closing) {
|
---|
25 | return default(T);
|
---|
26 | }
|
---|
27 | Monitor.Wait (queue);
|
---|
28 | }
|
---|
29 | return queue.Dequeue ();
|
---|
30 | }
|
---|
31 | }
|
---|
32 |
|
---|
33 | public void Close ()
|
---|
34 | {
|
---|
35 | lock (queue) {
|
---|
36 | closing = true;
|
---|
37 | Monitor.PulseAll (queue);
|
---|
38 | }
|
---|
39 | }
|
---|
40 |
|
---|
41 | }
|
---|
42 | }
|
---|
43 |
|
---|
Note:
See
TracBrowser
for help on using the repository browser.