1 using System;
2 using System.Collections.Generic;
3 using System.Threading;
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 class Distributor {
19 protected Distributor() {
20 for(uint k = 0; k<numberOfThreads_; k++) {
21 WorkerThread workerThread = new WorkerThread(k, this);
22 workerThread_.Add(workerThread);
23 workIsDoneEvents_[k] = workerThread.getWorkIsDoneEvent();
24 Thread thread = new Thread(new ThreadStart(workerThread.doWork));
25 thread.Start();
26 }
27 }
28
29 public void doWork() {
30 foreach (WorkerThread workerThread in workerThread_) workerThread.requestWork();
31 WaitHandle.WaitAll(workIsDoneEvents_);
32 }
33
34
35 public void doWork(uint workerNumber) {work_.execute(workerNumber);}
36
37 public void requestWorkerTermination() {
38 terminateThreads_ = true;
39 doWork();
40 }
41
42 public bool terminateThreads() {return terminateThreads_;}
43
44 public void set(Work work) {work_ = work;}
45
46 private Work work_;
47
48 private List<WorkerThread> workerThread_ = new List<WorkerThread>();
49 private WaitHandle[] workIsDoneEvents_ = new WaitHandle[numberOfThreads_];
50 private bool terminateThreads_ = false;
51
52 public static readonly uint numberOfThreads_ = (uint)Environment.ProcessorCount;
53 }
54
55 interface Work {
56 void execute(uint workerNumber);
57 }