-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSimpleBlockingQueue.cs
More file actions
81 lines (69 loc) · 3.2 KB
/
SimpleBlockingQueue.cs
File metadata and controls
81 lines (69 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System;
using System.Collections.Generic;
using System.Threading;
namespace AlgorithmsAndDataStructures.DataStructures.Concurrency;
public class SimpleBlockingQueue
{
private readonly object lockObject = new();
private readonly Queue<int> queue;
private readonly int size;
private int currentSize;
public SimpleBlockingQueue(int size = 8)
{
queue = new Queue<int>();
currentSize = 0;
this.size = size;
}
public void Enqueue(int value, TimeSpan timeout, CancellationToken cancellationToken)
{
try
{
Monitor.Enter(lockObject);
while (currentSize >= size)
{
if (cancellationToken.IsCancellationRequested) throw new OperationCanceledException(cancellationToken);
Monitor.Wait(lockObject, timeout);
}
queue.Enqueue(value);
currentSize++;
// Why do we need to call PulseAll instead of Pulse?
// Consider a situation with two producer threads and one consumer thread all working with a queue of size one.
// It's possible that when an item is added to the queue by one of the producer threads, the other two threads are blocked waiting on the condition variable.
// If the producer thread after adding an item invokes Pulse() it is possible that the other producer thread is chosen by the system to resume execution.
// The woken-up producer thread would find the queue full and go back to waiting on the condition variable, causing a deadlock.
// Invoking PulesAll() assures that the consumer thread also gets a chance to wake up and resume execution.
Monitor.PulseAll(lockObject);
}
finally
{
Monitor.Exit(lockObject);
}
}
public int Dequeue(TimeSpan timeout, CancellationToken cancellationToken)
{
int dequeued;
try
{
Monitor.Enter(lockObject);
while (currentSize <= 0)
{
if (cancellationToken.IsCancellationRequested) throw new OperationCanceledException(cancellationToken);
Monitor.Wait(lockObject, timeout);
}
dequeued = queue.Dequeue();
currentSize--;
// Why do we need to call PulseAll instead of Pulse?
// Consider a situation with two producer threads and one consumer thread all working with a queue of size one.
// It's possible that when an item is added to the queue by one of the producer threads, the other two threads are blocked waiting on the condition variable.
// If the producer thread after adding an item invokes Pulse() it is possible that the other producer thread is chosen by the system to resume execution.
// The woken-up producer thread would find the queue full and go back to waiting on the condition variable, causing a deadlock.
// Invoking PulesAll() assures that the consumer thread also gets a chance to wake up and resume execution.
Monitor.PulseAll(lockObject);
}
finally
{
Monitor.Exit(lockObject);
}
return dequeued;
}
}