From 520da595b7c4c04f345586ff98e5b9d9caf34eec Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 1 Oct 2023 14:09:31 +0800 Subject: [PATCH] feat: thread pool test demo --- CMakeLists.txt | 9 +++++---- src/core/CMakeLists.txt | 2 +- src/core_test/CMakeLists.txt | 8 -------- src/core_test/cases/all_cases.cc | 30 ++++++++++++++++++++++++++++++ third_party/third_party.cmake | 19 +++++++++++++++++++ 5 files changed, 55 insertions(+), 13 deletions(-) create mode 100644 third_party/third_party.cmake diff --git a/CMakeLists.txt b/CMakeLists.txt index c95354d..ee671d2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -8,7 +8,11 @@ endif() set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra") -get_filename_component(KLOTSKI_ROOT "${CMAKE_CURRENT_SOURCE_DIR}" ABSOLUTE) +get_filename_component(KLOTSKI_ROOT ${CMAKE_CURRENT_SOURCE_DIR} ABSOLUTE) + +set(KLOTSKI_UNIT_TEST ON) + +include(third_party/third_party.cmake) ################################################################################ @@ -28,9 +32,6 @@ if (BUILD_DYN) add_compile_options(-fPIC) endif() -set(ABSL_PROPAGATE_CXX_STD ON) -include_directories(third_party/abseil-cpp) -add_subdirectory(third_party/abseil-cpp) add_subdirectory(src) diff --git a/src/core/CMakeLists.txt b/src/core/CMakeLists.txt index c0fb277..6a92dfe 100644 --- a/src/core/CMakeLists.txt +++ b/src/core/CMakeLists.txt @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.0) set(CMAKE_CXX_STANDARD 20) -project(klotski-core VERSION 0.1.2 LANGUAGES CXX) +project(klotski-core VERSION 0.9.1 LANGUAGES CXX) add_compile_options(-fno-exceptions) diff --git a/src/core_test/CMakeLists.txt b/src/core_test/CMakeLists.txt index 166b835..f86645a 100644 --- a/src/core_test/CMakeLists.txt +++ b/src/core_test/CMakeLists.txt @@ -2,14 +2,6 @@ cmake_minimum_required(VERSION 3.0) set(CMAKE_CXX_STANDARD 20) -############################################################################################### - -include_directories(${KLOTSKI_ROOT}/third_party/md5sum) -add_subdirectory(${KLOTSKI_ROOT}/third_party/md5sum md5sum EXCLUDE_FROM_ALL) - -include_directories(${KLOTSKI_ROOT}/third_party/googletest/googletest/include) -add_subdirectory(${KLOTSKI_ROOT}/third_party/googletest googletest EXCLUDE_FROM_ALL) - set(KLOTSKI_TEST_DEPS klotski-core gtest gtest_main md5sum) ############################################################################################### diff --git a/src/core_test/cases/all_cases.cc b/src/core_test/cases/all_cases.cc index 61be112..d19ad04 100644 --- a/src/core_test/cases/all_cases.cc +++ b/src/core_test/cases/all_cases.cc @@ -5,6 +5,7 @@ #include "md5sum.h" #include "all_cases.h" #include "gtest/gtest.h" +#include "BS_thread_pool.hpp" using md5::md5sum; @@ -92,3 +93,32 @@ TEST(Cases, all_cases_data) { } // TODO: test all_cases_parallel_build + +TEST(Cases, thread_pool_demo) { + + BasicRanges::Instance().Build(); + + BS::thread_pool pool; + + std::cout << pool.get_thread_count() << std::endl; + + auto start = clock(); + auto start_ = std::chrono::high_resolution_clock::now(); + + AllCases::Instance().BuildParallel([&pool](std::function &&func) { +// std::cout << "receive new task" << std::endl; + pool.push_task(func); + }); + +// std::cout << "parallel build complete" << std::endl; + + pool.wait_for_tasks(); + + std::cerr << ((clock() - start) * 1000 / CLOCKS_PER_SEC) << "ms" << std::endl; + auto end = std::chrono::high_resolution_clock::now(); + auto elapsed = std::chrono::duration_cast(end - start_); + std::cerr << elapsed.count() / 1000 / 1000 << "ms" << std::endl; + +// std::cout << "pool tasks complete" << std::endl; + +} diff --git a/third_party/third_party.cmake b/third_party/third_party.cmake new file mode 100644 index 0000000..5ef8923 --- /dev/null +++ b/third_party/third_party.cmake @@ -0,0 +1,19 @@ +set(KLOTSKI_THIRD_PARTY ${KLOTSKI_ROOT}/third_party) + +set(ABSL_PROPAGATE_CXX_STD ON) +include_directories(abseil-cpp) +add_subdirectory(${KLOTSKI_THIRD_PARTY}/abseil-cpp EXCLUDE_FROM_ALL) + +if (KLOTSKI_UNIT_TEST) + include_directories(${KLOTSKI_THIRD_PARTY}/md5sum) + add_subdirectory(${KLOTSKI_THIRD_PARTY}/md5sum EXCLUDE_FROM_ALL) +endif (KLOTSKI_UNIT_TEST) + +if (KLOTSKI_UNIT_TEST) + include_directories(${KLOTSKI_THIRD_PARTY}/googletest/googletest/include) + add_subdirectory(${KLOTSKI_THIRD_PARTY}/googletest EXCLUDE_FROM_ALL) +endif (KLOTSKI_UNIT_TEST) + +if (KLOTSKI_UNIT_TEST) + include_directories(${KLOTSKI_THIRD_PARTY}/thread-pool/include) +endif (KLOTSKI_UNIT_TEST)