| 165 | } |
| 166 | |
| 167 | SC::Result SC::ThreadPool::waitForAllTasks() |
| 168 | { |
| 169 | // Function is entirely protected by the mutex |
| 170 | poolMutex.lock(); |
| 171 | auto deferUnlock = MakeDeferred([this] { poolMutex.unlock(); }); |
| 172 | if (numWorkerThreads == 0) |
| 173 | { |
| 174 | return Result(true); |
| 175 | } |
| 176 | for (;;) |
| 177 | { |
| 178 | const bool runningWithPendingTasks = not stopRequested and (taskHead != nullptr or numRunningTasks != 0); |
| 179 | const bool stoppingWithRunningThreads = stopRequested and numWorkerThreads != 0; |
| 180 | if (runningWithPendingTasks or stoppingWithRunningThreads) |
| 181 | { |
| 182 | taskCompleted.wait(poolMutex); |
| 183 | } |
| 184 | else |
| 185 | { |
| 186 | break; |
| 187 | } |
| 188 | } |
| 189 | return Result(true); |
| 190 | } |
| 191 | |
| 192 | SC::Result SC::ThreadPool::waitForTask(Task& task) |
| 193 | { |