Browse Source

feat: thread pool test demo

master
Dnomd343 1 year ago
parent
commit
520da595b7
  1. 9
      CMakeLists.txt
  2. 2
      src/core/CMakeLists.txt
  3. 8
      src/core_test/CMakeLists.txt
  4. 30
      src/core_test/cases/all_cases.cc
  5. 19
      third_party/third_party.cmake

9
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)

2
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)

8
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)
###############################################################################################

30
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<void()> &&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<std::chrono::nanoseconds>(end - start_);
std::cerr << elapsed.count() / 1000 / 1000 << "ms" << std::endl;
// std::cout << "pool tasks complete" << std::endl;
}

19
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)
Loading…
Cancel
Save