1 /* 2 Copyright (c) 2019-2021 Timur Gafarov 3 4 Boost Software License - Version 1.0 - August 17th, 2003 5 6 Permission is hereby granted, free of charge, to any person or organization 7 obtaining a copy of the software and accompanying documentation covered by 8 this license (the "Software") to use, reproduce, display, distribute, 9 execute, and transmit the Software, and to prepare derivative works of the 10 Software, and to permit third-parties to whom the Software is furnished to 11 do so, all subject to the following: 12 13 The copyright notices in the Software and this entire statement, including 14 the above license grant, this restriction and the following disclaimer, 15 must be included in all copies of the Software, in whole or in part, and 16 all derivative works of the Software, unless such copies or derivative 17 works are solely in the form of machine-executable object code generated by 18 a source language processor. 19 20 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 21 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 22 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT 23 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE 24 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE, 25 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 26 DEALINGS IN THE SOFTWARE. 27 */ 28 29 /** 30 * Copyright: Timur Gafarov 2019-2021. 31 * License: $(LINK2 boost.org/LICENSE_1_0.txt, Boost License 1.0). 32 * Authors: Timur Gafarov 33 */ 34 module dlib.concurrency.threadpool; 35 36 import std.functional; 37 import dlib.core.memory; 38 import dlib.core.mutex; 39 import dlib.concurrency.workerthread; 40 import dlib.concurrency.taskqueue; 41 42 /** 43 * An object that manages worker threads and runs tasks on them 44 */ 45 class ThreadPool 46 { 47 protected: 48 uint maxThreads; 49 WorkerThread[] workerThreads; 50 TaskQueue taskQueue; 51 bool running = true; 52 Mutex mutex; 53 54 public: 55 56 /// Constructor 57 this(uint maxThreads) 58 { 59 this.maxThreads = maxThreads; 60 workerThreads = New!(WorkerThread[])(maxThreads); 61 taskQueue = New!TaskQueue(); 62 63 mutex.init(); 64 65 foreach(i, ref t; workerThreads) 66 { 67 t = New!WorkerThread(i, this); 68 t.start(); 69 } 70 } 71 72 ~this() 73 { 74 mutex.lock(); 75 running = false; 76 mutex.unlock(); 77 78 foreach(i, ref t; workerThreads) 79 { 80 t.join(); 81 Delete(t); 82 } 83 84 Delete(taskQueue); 85 Delete(workerThreads); 86 87 mutex.destroy(); 88 } 89 90 /// Create a task from delegate 91 Task submit(void delegate() taskDele) 92 { 93 Task task = Task(TaskState.Valid, taskDele); 94 if (!taskQueue.enqueue(task)) 95 { 96 task.run(); 97 } 98 return task; 99 } 100 101 /// Create a task from function pointer 102 Task submit(void function() taskFunc) 103 { 104 return submit(toDelegate(taskFunc)); 105 } 106 107 Task request() 108 { 109 return taskQueue.dequeue(); 110 } 111 112 bool isRunning() 113 { 114 return running; 115 } 116 117 /// Returns true if all tasks are finished 118 bool tasksDone() 119 { 120 if (taskQueue.count == 0) 121 { 122 foreach(i, t; workerThreads) 123 { 124 if (t.busy) 125 return false; 126 } 127 128 return true; 129 } 130 else 131 return false; 132 } 133 } 134 135 /// 136 unittest 137 { 138 import std.stdio; 139 140 int x = 0; 141 int y = 0; 142 143 void task1() 144 { 145 while(x < 100) 146 x += 1; 147 } 148 149 void task2() 150 { 151 while(y < 100) 152 y += 1; 153 } 154 155 ThreadPool threadPool = New!ThreadPool(2); 156 157 threadPool.submit(&task1); 158 threadPool.submit(&task2); 159 160 while(!threadPool.tasksDone) {} 161 162 if (x != 100) writeln(x); 163 if (y != 100) writeln(y); 164 165 assert(x == 100); 166 assert(y == 100); 167 168 Delete(threadPool); 169 }