blob: 02d116e05b43d94b7fe999bf966b8ac020b9c9c0 (
plain)
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
|
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Tango.Core
{
/// <summary>
/// Represents a blocking concurrent queue for a producer consumer multi threading scenario.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <seealso cref="System.Collections.Concurrent.BlockingCollection{T}" />
public class ProducerConsumerQueue<T> : BlockingCollection<T>
{
/// <summary>
/// Initializes a new instance of the ProducerConsumerQueue, Use Add and TryAdd for Enqueue and TryEnqueue and Take and TryTake for Dequeue and TryDequeue functionality
/// </summary>
public ProducerConsumerQueue()
: base(new ConcurrentQueue<T>())
{
}
/// <summary>
/// Initializes a new instance of the ProducerConsumerQueue, Use Add and TryAdd for Enqueue and TryEnqueue and Take and TryTake for Dequeue and TryDequeue functionality
/// </summary>
/// <param name="maxSize"></param>
public ProducerConsumerQueue(int maxSize)
: base(new ConcurrentQueue<T>(), maxSize)
{
}
/// <summary>
/// Enqueues the specified item.
/// </summary>
/// <param name="item">The item.</param>
public void BlockEnqueue(T item)
{
Add(item);
}
/// <summary>
/// Blocks until an item is available for dequeuing.
/// </summary>
/// <returns></returns>
public T BlockDequeue()
{
return Take();
}
/// <summary>
/// Blocks until an item is available for dequeuing.
/// </summary>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns></returns>
public T BlockDequeue(CancellationToken cancellationToken)
{
return Take(cancellationToken);
}
}
}
|