thread-pool.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. /* Thread pool
  2. Copyright (C) 2019-2022 Free Software Foundation, Inc.
  3. This file is part of GDB.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef GDBSUPPORT_THREAD_POOL_H
  15. #define GDBSUPPORT_THREAD_POOL_H
  16. #include <queue>
  17. #include <thread>
  18. #include <vector>
  19. #include <functional>
  20. #include <mutex>
  21. #include <condition_variable>
  22. #include <future>
  23. #include "gdbsupport/gdb_optional.h"
  24. namespace gdb
  25. {
  26. /* A thread pool.
  27. There is a single global thread pool, see g_thread_pool. Tasks can
  28. be submitted to the thread pool. They will be processed in worker
  29. threads as time allows. */
  30. class thread_pool
  31. {
  32. public:
  33. /* The sole global thread pool. */
  34. static thread_pool *g_thread_pool;
  35. ~thread_pool ();
  36. DISABLE_COPY_AND_ASSIGN (thread_pool);
  37. /* Set the thread count of this thread pool. By default, no threads
  38. are created -- the thread count must be set first. */
  39. void set_thread_count (size_t num_threads);
  40. /* Return the number of executing threads. */
  41. size_t thread_count () const
  42. {
  43. return m_thread_count;
  44. }
  45. /* Post a task to the thread pool. A future is returned, which can
  46. be used to wait for the result. */
  47. std::future<void> post_task (std::function<void ()> &&func);
  48. private:
  49. thread_pool () = default;
  50. /* The callback for each worker thread. */
  51. void thread_function ();
  52. /* The current thread count. */
  53. size_t m_thread_count = 0;
  54. /* A convenience typedef for the type of a task. */
  55. typedef std::packaged_task<void ()> task;
  56. /* The tasks that have not been processed yet. An optional is used
  57. to represent a task. If the optional is empty, then this means
  58. that the receiving thread should terminate. If the optional is
  59. non-empty, then it is an actual task to evaluate. */
  60. std::queue<optional<task>> m_tasks;
  61. /* A condition variable and mutex that are used for communication
  62. between the main thread and the worker threads. */
  63. std::condition_variable m_tasks_cv;
  64. std::mutex m_tasks_mutex;
  65. };
  66. }
  67. #endif /* GDBSUPPORT_THREAD_POOL_H */