-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFixedSizeThreadPool.cs
More file actions
51 lines (47 loc) · 977 Bytes
/
FixedSizeThreadPool.cs
File metadata and controls
51 lines (47 loc) · 977 Bytes
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
using System;
using System.Collections.Generic;
using System.Threading;
namespace FileSignature
{
internal static class FixedSizeThreadPool
{
private static readonly Queue<Action> Work;
private static readonly List<Thread> Threads;
static FixedSizeThreadPool()
{
Work = new Queue<Action>();
Threads = new List<Thread>();
int processorCount = Environment.ProcessorCount;
for (int i = 0; i < processorCount; ++i)
{
var thread = new Thread(DoWork) {IsBackground = true};
thread.Start();
Threads.Add(thread);
}
}
private static void DoWork()
{
while (true)
{
Action work;
lock (Work)
{
while (Work.Count == 0)
Monitor.Wait(Work);
work = Work.Dequeue();
}
work();
}
}
public static void QueueAction(Action action)
{
if (action == null)
throw new ArgumentNullException(nameof(action));
lock (Work)
{
Work.Enqueue(action);
Monitor.Pulse(Work);
}
}
}
}