thread_pool_test.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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. #include "ceres/thread_pool.h"
  31. #include <chrono>
  32. #include <condition_variable>
  33. #include <mutex>
  34. #include <thread>
  35. #include "ceres/internal/config.h"
  36. #include "glog/logging.h"
  37. #include "gmock/gmock.h"
  38. #include "gtest/gtest.h"
  39. namespace ceres::internal {
  40. // Adds a number of tasks to the thread pool and ensures they all run.
  41. TEST(ThreadPool, AddTask) {
  42. int value = 0;
  43. const int num_tasks = 100;
  44. {
  45. ThreadPool thread_pool(2);
  46. std::condition_variable condition;
  47. std::mutex mutex;
  48. for (int i = 0; i < num_tasks; ++i) {
  49. thread_pool.AddTask([&]() {
  50. std::lock_guard<std::mutex> lock(mutex);
  51. ++value;
  52. condition.notify_all();
  53. });
  54. }
  55. std::unique_lock<std::mutex> lock(mutex);
  56. condition.wait(lock, [&]() { return value == num_tasks; });
  57. }
  58. EXPECT_EQ(num_tasks, value);
  59. }
  60. // Adds a number of tasks to the queue and resizes the thread pool while the
  61. // threads are executing their work.
  62. TEST(ThreadPool, ResizingDuringExecution) {
  63. int value = 0;
  64. const int num_tasks = 100;
  65. // Run this test in a scope to delete the thread pool and all of the threads
  66. // are stopped.
  67. {
  68. ThreadPool thread_pool(/*num_threads=*/2);
  69. std::condition_variable condition;
  70. std::mutex mutex;
  71. // Acquire a lock on the mutex to prevent the threads from finishing their
  72. // execution so we can test resizing the thread pool while the workers are
  73. // executing a task.
  74. std::unique_lock<std::mutex> lock(mutex);
  75. // The same task for all of the workers to execute.
  76. auto task = [&]() {
  77. // This will block until the mutex is released inside the condition
  78. // variable.
  79. std::lock_guard<std::mutex> lock(mutex);
  80. ++value;
  81. condition.notify_all();
  82. };
  83. // Add the initial set of tasks to run.
  84. for (int i = 0; i < num_tasks / 2; ++i) {
  85. thread_pool.AddTask(task);
  86. }
  87. // Resize the thread pool while tasks are executing.
  88. thread_pool.Resize(/*num_threads=*/3);
  89. // Add more tasks to the thread pool to guarantee these are also completed.
  90. for (int i = 0; i < num_tasks / 2; ++i) {
  91. thread_pool.AddTask(task);
  92. }
  93. // Unlock the mutex to unblock all of the threads and wait until all of the
  94. // tasks are completed.
  95. condition.wait(lock, [&]() { return value == num_tasks; });
  96. }
  97. EXPECT_EQ(num_tasks, value);
  98. }
  99. // Tests the destructor will wait until all running tasks are finished before
  100. // destructing the thread pool.
  101. TEST(ThreadPool, Destructor) {
  102. // Ensure the hardware supports more than 1 thread to ensure the test will
  103. // pass.
  104. const int num_hardware_threads = std::thread::hardware_concurrency();
  105. if (num_hardware_threads <= 1) {
  106. LOG(ERROR)
  107. << "Test not supported, the hardware does not support threading.";
  108. return;
  109. }
  110. std::condition_variable condition;
  111. std::mutex mutex;
  112. // Lock the mutex to ensure the tasks are blocked.
  113. std::unique_lock<std::mutex> master_lock(mutex);
  114. int value = 0;
  115. // Create a thread that will instantiate and delete the thread pool. This is
  116. // required because we need to block on the thread pool being deleted and
  117. // signal the tasks to finish.
  118. std::thread thread([&]() {
  119. ThreadPool thread_pool(/*num_threads=*/2);
  120. for (int i = 0; i < 100; ++i) {
  121. thread_pool.AddTask([&]() {
  122. // This will block until the mutex is released inside the condition
  123. // variable.
  124. std::lock_guard<std::mutex> lock(mutex);
  125. ++value;
  126. condition.notify_all();
  127. });
  128. }
  129. // The thread pool should be deleted.
  130. });
  131. // Give the thread pool time to start, add all the tasks, and then delete
  132. // itself.
  133. std::this_thread::sleep_for(std::chrono::milliseconds(500));
  134. // Unlock the tasks.
  135. master_lock.unlock();
  136. // Wait for the thread to complete.
  137. thread.join();
  138. EXPECT_EQ(100, value);
  139. }
  140. TEST(ThreadPool, Resize) {
  141. // Ensure the hardware supports more than 1 thread to ensure the test will
  142. // pass.
  143. const int num_hardware_threads = std::thread::hardware_concurrency();
  144. if (num_hardware_threads <= 1) {
  145. LOG(ERROR)
  146. << "Test not supported, the hardware does not support threading.";
  147. return;
  148. }
  149. ThreadPool thread_pool(1);
  150. EXPECT_EQ(1, thread_pool.Size());
  151. thread_pool.Resize(2);
  152. EXPECT_EQ(2, thread_pool.Size());
  153. // Try reducing the thread pool size and verify it stays the same size.
  154. thread_pool.Resize(1);
  155. EXPECT_EQ(2, thread_pool.Size());
  156. }
  157. } // namespace ceres::internal