123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192 |
- #include "ceres/thread_pool.h"
- #include <chrono>
- #include <condition_variable>
- #include <mutex>
- #include <thread>
- #include "ceres/internal/config.h"
- #include "glog/logging.h"
- #include "gmock/gmock.h"
- #include "gtest/gtest.h"
- namespace ceres::internal {
- TEST(ThreadPool, AddTask) {
- int value = 0;
- const int num_tasks = 100;
- {
- ThreadPool thread_pool(2);
- std::condition_variable condition;
- std::mutex mutex;
- for (int i = 0; i < num_tasks; ++i) {
- thread_pool.AddTask([&]() {
- std::lock_guard<std::mutex> lock(mutex);
- ++value;
- condition.notify_all();
- });
- }
- std::unique_lock<std::mutex> lock(mutex);
- condition.wait(lock, [&]() { return value == num_tasks; });
- }
- EXPECT_EQ(num_tasks, value);
- }
- TEST(ThreadPool, ResizingDuringExecution) {
- int value = 0;
- const int num_tasks = 100;
-
-
- {
- ThreadPool thread_pool(2);
- std::condition_variable condition;
- std::mutex mutex;
-
-
-
- std::unique_lock<std::mutex> lock(mutex);
-
- auto task = [&]() {
-
-
- std::lock_guard<std::mutex> lock(mutex);
- ++value;
- condition.notify_all();
- };
-
- for (int i = 0; i < num_tasks / 2; ++i) {
- thread_pool.AddTask(task);
- }
-
- thread_pool.Resize(3);
-
- for (int i = 0; i < num_tasks / 2; ++i) {
- thread_pool.AddTask(task);
- }
-
-
- condition.wait(lock, [&]() { return value == num_tasks; });
- }
- EXPECT_EQ(num_tasks, value);
- }
- TEST(ThreadPool, Destructor) {
-
-
- const int num_hardware_threads = std::thread::hardware_concurrency();
- if (num_hardware_threads <= 1) {
- LOG(ERROR)
- << "Test not supported, the hardware does not support threading.";
- return;
- }
- std::condition_variable condition;
- std::mutex mutex;
-
- std::unique_lock<std::mutex> master_lock(mutex);
- int value = 0;
-
-
-
- std::thread thread([&]() {
- ThreadPool thread_pool(2);
- for (int i = 0; i < 100; ++i) {
- thread_pool.AddTask([&]() {
-
-
- std::lock_guard<std::mutex> lock(mutex);
- ++value;
- condition.notify_all();
- });
- }
-
- });
-
-
- std::this_thread::sleep_for(std::chrono::milliseconds(500));
-
- master_lock.unlock();
-
- thread.join();
- EXPECT_EQ(100, value);
- }
- TEST(ThreadPool, Resize) {
-
-
- const int num_hardware_threads = std::thread::hardware_concurrency();
- if (num_hardware_threads <= 1) {
- LOG(ERROR)
- << "Test not supported, the hardware does not support threading.";
- return;
- }
- ThreadPool thread_pool(1);
- EXPECT_EQ(1, thread_pool.Size());
- thread_pool.Resize(2);
- EXPECT_EQ(2, thread_pool.Size());
-
- thread_pool.Resize(1);
- EXPECT_EQ(2, thread_pool.Size());
- }
- }
|