threadpool.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 task = std::make_shared<std::packaged_task<R()>>(
  63. std::bind(std::forward<F>(f), std::forward<Args>(args)...));
  64. std::future<R> result = task->get_future();
  65. mTasks.emplace([task = std::move(task), token = Init::Token()]() { return (*task)(); });
  66. mCondition.notify_one();
  67. return result;
  68. }
  69. } // namespace rtc
  70. #endif