| 366 | |
| 367 | template <class func_t, class... args_t> |
| 368 | auto ThreadPool::AddTask(func_t&& f, args_t&&... args) |
| 369 | -> std::shared_future<result_of_t<func_t, args_t...>> { |
| 370 | using return_t = result_of_t<func_t, args_t...>; |
| 371 | |
| 372 | auto task = std::make_shared<std::packaged_task<return_t()>>( |
| 373 | std::bind(std::forward<func_t>(f), std::forward<args_t>(args)...)); |
| 374 | |
| 375 | std::shared_future<return_t> result = task->get_future().share(); |
| 376 | |
| 377 | { |
| 378 | std::unique_lock<std::mutex> lock(mutex_); |
| 379 | if (stopped_) { |
| 380 | throw std::runtime_error("Cannot add task to stopped thread pool."); |
| 381 | } |
| 382 | tasks_.emplace([task = std::move(task), result]() { |
| 383 | (*task)(); |
| 384 | return [result = std::move(result)]() { result.get(); }; |
| 385 | }); |
| 386 | } |
| 387 | |
| 388 | task_condition_.notify_one(); |
| 389 | |
| 390 | return result; |
| 391 | } |
| 392 | |
| 393 | template <typename T> |
| 394 | JobQueue<T>::JobQueue() : JobQueue(std::numeric_limits<size_t>::max()) {} |
no outgoing calls