threadpool.hpp 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /**
  2. * Copyright (c) 2020 Paul-Louis Ageneau
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * This library is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with this library; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #ifndef RTC_THREADPOOL_H
  19. #define RTC_THREADPOOL_H
  20. #include "include.hpp"
  21. #include "init.hpp"
  22. #include <condition_variable>
  23. #include <functional>
  24. #include <future>
  25. #include <memory>
  26. #include <mutex>
  27. #include <queue>
  28. #include <stdexcept>
  29. #include <thread>
  30. #include <vector>
  31. namespace rtc {
  32. template <class F, class... Args>
  33. using invoke_future_t = std::future<std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>>;
  34. class ThreadPool final {
  35. public:
  36. static ThreadPool &Instance();
  37. ThreadPool(const ThreadPool &) = delete;
  38. ThreadPool &operator=(const ThreadPool &) = delete;
  39. ThreadPool(ThreadPool &&) = delete;
  40. ThreadPool &operator=(ThreadPool &&) = delete;
  41. int count() const;
  42. void spawn(int count = 1);
  43. void join();
  44. void run();
  45. bool runOne();
  46. template <class F, class... Args>
  47. auto enqueue(F &&f, Args &&... args) -> invoke_future_t<F, Args...>;
  48. protected:
  49. ThreadPool() = default;
  50. ~ThreadPool();
  51. std::function<void()> dequeue(); // returns null function if joining
  52. std::vector<std::thread> mWorkers;
  53. std::queue<std::function<void()>> mTasks;
  54. std::atomic<bool> mJoining = false;
  55. mutable std::mutex mMutex, mWorkersMutex;
  56. std::condition_variable mCondition;
  57. };
  58. template <class F, class... Args>
  59. auto ThreadPool::enqueue(F &&f, Args &&... args) -> invoke_future_t<F, Args...> {
  60. std::unique_lock lock(mMutex);
  61. using R = std::invoke_result_t<std::decay_t<F>, std::decay_t<Args>...>;
  62. auto bound = std::bind(std::forward<F>(f), std::forward<Args>(args)...);
  63. auto task = std::make_shared<std::packaged_task<R()>>([bound = std::move(bound)]() mutable {
  64. try {
  65. return bound();
  66. } catch (const std::exception &e) {
  67. PLOG_WARNING << e.what();
  68. throw;
  69. }
  70. });
  71. std::future<R> result = task->get_future();
  72. mTasks.emplace([task = std::move(task), token = Init::Token()]() { return (*task)(); });
  73. mCondition.notify_one();
  74. return result;
  75. }
  76. } // namespace rtc
  77. #endif