-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathParallel.cs
134 lines (124 loc) · 4.01 KB
/
Parallel.cs
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
namespace camera_connection
{
using System;
using System.Runtime.CompilerServices;
using System.Threading;
public sealed class Parallel
{
private int currentIndex;
private static volatile Parallel instance = null;
private AutoResetEvent[] jobAvailable;
private ForLoopBody loopBody;
private int stopIndex;
private static object sync = new object();
private ManualResetEvent[] threadIdle;
private Thread[] threads;
private static int threadsCount = Environment.ProcessorCount;
private Parallel()
{
}
public static void For(int start, int stop, ForLoopBody loopBody)
{
lock (sync)
{
Parallel instance = Instance;
instance.currentIndex = start - 1;
instance.stopIndex = stop;
instance.loopBody = loopBody;
for (int i = 0; i < threadsCount; i++)
{
instance.threadIdle[i].Reset();
instance.jobAvailable[i].Set();
}
for (int j = 0; j < threadsCount; j++)
{
instance.threadIdle[j].WaitOne();
}
}
}
private void Initialize()
{
this.jobAvailable = new AutoResetEvent[threadsCount];
this.threadIdle = new ManualResetEvent[threadsCount];
this.threads = new Thread[threadsCount];
for (int i = 0; i < threadsCount; i++)
{
this.jobAvailable[i] = new AutoResetEvent(false);
this.threadIdle[i] = new ManualResetEvent(true);
this.threads[i] = new Thread(new ParameterizedThreadStart(this.WorkerThread));
this.threads[i].Name = "AForge.Parallel";
this.threads[i].IsBackground = true;
this.threads[i].Start(i);
}
}
private void Terminate()
{
this.loopBody = null;
int index = 0;
int length = this.threads.Length;
while (index < length)
{
this.jobAvailable[index].Set();
this.threads[index].Join();
this.jobAvailable[index].Close();
this.threadIdle[index].Close();
index++;
}
this.jobAvailable = null;
this.threadIdle = null;
this.threads = null;
}
private void WorkerThread(object index)
{
int num = (int) index;
int num2 = 0;
Label_0009:
this.jobAvailable[num].WaitOne();
if (this.loopBody == null)
{
return;
}
Label_0020:
num2 = Interlocked.Increment(ref this.currentIndex);
if (num2 < this.stopIndex)
{
this.loopBody(num2);
goto Label_0020;
}
this.threadIdle[num].Set();
goto Label_0009;
}
private static Parallel Instance
{
get
{
if (instance == null)
{
instance = new Parallel();
instance.Initialize();
}
else if (instance.threads.Length != threadsCount)
{
instance.Terminate();
instance.Initialize();
}
return instance;
}
}
public static int ThreadsCount
{
get
{
return threadsCount;
}
set
{
lock (sync)
{
threadsCount = Math.Max(1, value);
}
}
}
public delegate void ForLoopBody(int index);
}
}