Add a threaded task, blocking until a thread is available. If join() is called, add() may not be called again until go() is called and completes.
| 127 | // Add a threaded task, blocking until a thread is available. If join() is |
| 128 | // called, add() may not be called again until go() is called and completes. |
| 129 | PDAL_EXPORT void add(std::function<void()> task) |
| 130 | { |
| 131 | std::unique_lock<std::mutex> lock(m_mutex); |
| 132 | if (!m_running) |
| 133 | { |
| 134 | throw pdal_error("Attempted to add a task to a stopped ThreadPool"); |
| 135 | } |
| 136 | |
| 137 | m_produceCv.wait(lock, [this]() |
| 138 | { |
| 139 | return m_queueSize < 0 || m_tasks.size() < (size_t)m_queueSize; |
| 140 | }); |
| 141 | |
| 142 | m_tasks.emplace(task); |
| 143 | |
| 144 | // Notify worker that a task is available. |
| 145 | lock.unlock(); |
| 146 | m_consumeCv.notify_all(); |
| 147 | } |
| 148 | |
| 149 | PDAL_EXPORT std::size_t size() const |
| 150 | { return m_numThreads; } |
no test coverage detected