| 103 | } |
| 104 | |
| 105 | WorkerPoolHandle WorkerPool::addWork(function<void()> work) { |
| 106 | // Construct a worker pool handle and wrap the work to signal the handle when |
| 107 | // finished. Set the result to empty string if successful and to the content |
| 108 | // of the exception if an exception is thrown. |
| 109 | auto workerPoolHandleImpl = make_shared<WorkerPoolHandle::Impl>(); |
| 110 | queueWork([workerPoolHandleImpl, work]() { |
| 111 | try { |
| 112 | work(); |
| 113 | MutexLocker handleLocker(workerPoolHandleImpl->mutex); |
| 114 | workerPoolHandleImpl->done = true; |
| 115 | workerPoolHandleImpl->condition.broadcast(); |
| 116 | } catch (...) { |
| 117 | MutexLocker handleLocker(workerPoolHandleImpl->mutex); |
| 118 | workerPoolHandleImpl->done = true; |
| 119 | workerPoolHandleImpl->exception = std::current_exception(); |
| 120 | workerPoolHandleImpl->condition.broadcast(); |
| 121 | } |
| 122 | }); |
| 123 | |
| 124 | return workerPoolHandleImpl; |
| 125 | } |
| 126 | |
| 127 | WorkerPool::WorkerThread::WorkerThread(WorkerPool* parent) |
| 128 | : Thread(strf("WorkerThread for WorkerPool '{}'", parent->m_name)), |