Introduced
In the previous C # Implementation of the thread pool function (a) , we basically implemented a program that can execute. And not really called a thread pool. Because the code in the previous article has a fatal bug that is that there is no task is not waiting, but crazy to Do While loop, and try to lock task linked list, this brings the problem is the performance is very low, the program reflects very slow (after adding a new task, It will take a long time for the job to start executing) The cause is just what was said.
In order to solve the problem, we need to use some method to make the process synchronous.
Method One
Using semaphores
In order to reduce the lock operation on task tasks, we only test when the task is not empty.
Our signal volume represents the number of tasks in the table. When S. WaitOne (); We started to lock up and take out the mission after the success.
while (flag && taskqueue! = null) { //wait for Task threadpoolmanager.s.waitone (); Get Task Lock (Taskqueue) { try { if (Taskqueue.count > 0) task = Taskqueue.dequeue (); else task = null; } catch (Exception) { task = null; } if (task = = null) continue; }
Add two variables to the Threadpoolmanager class
Because the semaphore needs to define a public int maxjobnum = +; public static Semaphore s;
And initialize the semaphore when initializing this class S = new Semaphore (0, Maxjobnum);
This will enable synchronization.
A test class is given below
static void Main (string[] args) { Threadpoolmanager TPM = new Threadpoolmanager (2); Testtask T1 = new Testtask ("Task1"); Testtask t2 = new Testtask ("Task2"); testtask t3 = new Testtask ("Task3"); testtask T4 = new Testtask ("Task4"); Testtask T5 = new Testtask ("Task5"); Testtask T6 = new Testtask ("Task6"); Testtask t7 = new Testtask ("Task7"); Testtask T8 = new Testtask ("Task8"); Testtask T9 = new Testtask ("TASK9"); Tpm. AddTask (t1); Tpm. AddTask (T2); Tpm. AddTask (T3); Tpm. AddTask (T4); Tpm. AddTask (T5); Tpm. AddTask (T6); Tpm. AddTask (T7); Tpm. AddTask (T8); Tpm. AddTask (T9); }
Method Two we are not using the semaphore. We use AutoResetEvent to achieve synchronization
The first step. Creates a locks = new AutoResetEvent (false) when Threadpoolmanager is initialized, Locks.set () when AddTask, and notifies the waiting operation.
Then we make a little change to the run function of Workthread.
public void Run () {while (flag && taskqueue! = null) {//Wait for task ThreadPoolManager.sep.WaitOne (); Wait for task while (Taskqueue.count = = 0 && flag) {Try {ThreadPoolManager.locks.WaitOne (); } catch (Exception) {}}//Get task Lock (Taskqueue) {try {task = T Askqueue.dequeue (); } catch (Exception) {task = null; } if (task = = null) continue; } try {task. SetEnd (FALSE); Task. StartTask ();} catch (Exception) {} try { if (!task. Isend ()) {task. SetEnd (FALSE); Task. Endtask (); }} catch (Exception) {}}//end of While}Only when the number of task lists is 0 o'clock we jam until addtask.
C # itself implements thread pool function (ii)