| 133 | } |
| 134 | |
| 135 | SC::Result SC::ThreadPool::destroy() |
| 136 | { |
| 137 | { |
| 138 | poolMutex.lock(); |
| 139 | auto deferUnlock = MakeDeferred([this] { poolMutex.unlock(); }); |
| 140 | if (numWorkerThreads == 0) |
| 141 | { |
| 142 | return Result(true); // this was already destroyed |
| 143 | } |
| 144 | // 1. Free tasks that have not been executed yet |
| 145 | while (taskHead) |
| 146 | { |
| 147 | Task* task = taskHead->next; |
| 148 | taskHead->next = nullptr; |
| 149 | taskHead->threadPool = nullptr; |
| 150 | taskHead = task; |
| 151 | } |
| 152 | taskTail = nullptr; |
| 153 | |
| 154 | // 2. Request all threads to stop |
| 155 | stopRequested = true; |
| 156 | taskAvailable.broadcast(); |
| 157 | } |
| 158 | |
| 159 | // 3. Wait for all tasks to stop |
| 160 | Result res = waitForAllTasks(); |
| 161 | |
| 162 | // 4. Reset the stop flag |
| 163 | stopRequested = false; |
| 164 | return res; |
| 165 | } |
| 166 | |
| 167 | SC::Result SC::ThreadPool::waitForAllTasks() |
| 168 | { |
nothing calls this directly
no test coverage detected