thread_pool.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  3. // http://ceres-solver.org/
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. //
  8. // * Redistributions of source code must retain the above copyright notice,
  9. // this list of conditions and the following disclaimer.
  10. // * Redistributions in binary form must reproduce the above copyright notice,
  11. // this list of conditions and the following disclaimer in the documentation
  12. // and/or other materials provided with the distribution.
  13. // * Neither the name of Google Inc. nor the names of its contributors may be
  14. // used to endorse or promote products derived from this software without
  15. // specific prior written permission.
  16. //
  17. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  18. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  21. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  22. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  23. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  24. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  25. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  26. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  27. // POSSIBILITY OF SUCH DAMAGE.
  28. //
  29. // Author: vitus@google.com (Michael Vitus)
  30. #ifndef CERES_INTERNAL_THREAD_POOL_H_
  31. #define CERES_INTERNAL_THREAD_POOL_H_
  32. #include <functional>
  33. #include <mutex>
  34. #include <thread>
  35. #include <vector>
  36. #include "ceres/concurrent_queue.h"
  37. #include "ceres/internal/export.h"
  38. namespace ceres::internal {
  39. // A thread-safe thread pool with an unbounded task queue and a resizable number
  40. // of workers. The size of the thread pool can be increased but never decreased
  41. // in order to support the largest number of threads requested. The ThreadPool
  42. // has three states:
  43. //
  44. // (1) The thread pool size is zero. Tasks may be added to the thread pool via
  45. // AddTask but they will not be executed until the thread pool is resized.
  46. //
  47. // (2) The thread pool size is greater than zero. Tasks may be added to the
  48. // thread pool and will be executed as soon as a worker is available. The
  49. // thread pool may be resized while the thread pool is running.
  50. //
  51. // (3) The thread pool is destructing. The thread pool will signal all the
  52. // workers to stop. The workers will finish all of the tasks that have already
  53. // been added to the thread pool.
  54. //
  55. class CERES_NO_EXPORT ThreadPool {
  56. public:
  57. // Returns the maximum number of hardware threads.
  58. static int MaxNumThreadsAvailable();
  59. // Default constructor with no active threads. We allow instantiating a
  60. // thread pool with no threads to support the use case of single threaded
  61. // Ceres where everything will be executed on the main thread. For single
  62. // threaded execution this has two benefits: avoid any overhead as threads
  63. // are expensive to create, and no unused threads shown in the debugger.
  64. ThreadPool();
  65. // Instantiates a thread pool with min(MaxNumThreadsAvailable, num_threads)
  66. // number of threads.
  67. explicit ThreadPool(int num_threads);
  68. // Signals the workers to stop and waits for them to finish any tasks that
  69. // have been scheduled.
  70. ~ThreadPool();
  71. // Resizes the thread pool if it is currently less than the requested number
  72. // of threads. The thread pool will be resized to min(MaxNumThreadsAvailable,
  73. // num_threads) number of threads. Resize does not support reducing the
  74. // thread pool size. If a smaller number of threads is requested, the thread
  75. // pool remains the same size. The thread pool is reused within Ceres with
  76. // different number of threads, and we need to ensure we can support the
  77. // largest number of threads requested. It is safe to resize the thread pool
  78. // while the workers are executing tasks, and the resizing is guaranteed to
  79. // complete upon return.
  80. void Resize(int num_threads);
  81. // Adds a task to the queue and wakes up a blocked thread. If the thread pool
  82. // size is greater than zero, then the task will be executed by a currently
  83. // idle thread or when a thread becomes available. If the thread pool has no
  84. // threads, then the task will never be executed and the user should use
  85. // Resize() to create a non-empty thread pool.
  86. void AddTask(const std::function<void()>& func);
  87. // Returns the current size of the thread pool.
  88. int Size();
  89. private:
  90. // Main loop for the threads which blocks on the task queue until work becomes
  91. // available. It will return if and only if Stop has been called.
  92. void ThreadMainLoop();
  93. // Signal all the threads to stop. It does not block until the threads are
  94. // finished.
  95. void Stop();
  96. // The queue that stores the units of work available for the thread pool. The
  97. // task queue maintains its own thread safety.
  98. ConcurrentQueue<std::function<void()>> task_queue_;
  99. std::vector<std::thread> thread_pool_;
  100. std::mutex thread_pool_mutex_;
  101. };
  102. } // namespace ceres::internal
  103. #endif // CERES_INTERNAL_THREAD_POOL_H_