From 2caa33b5ab8eb0a9fe018fdcdb50a293023c84a1 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Sun, 8 Jan 2023 00:53:06 +0800 Subject: [PATCH] feat: building mutex in AllCases --- all_cases/CMakeLists.txt | 1 + all_cases/all_cases.cc | 42 +++++++++++++++++++++++++++++++++++++--- all_cases/all_cases.h | 5 +++++ all_cases/main.cc | 16 +++++++++++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/all_cases/CMakeLists.txt b/all_cases/CMakeLists.txt index 6929d98..f3cc81a 100644 --- a/all_cases/CMakeLists.txt +++ b/all_cases/CMakeLists.txt @@ -2,3 +2,4 @@ cmake_minimum_required(VERSION 3.0) set(CMAKE_CXX_STANDARD 14) add_executable(klotski main.cc all_cases.cc) +target_link_libraries(klotski pthread) diff --git a/all_cases/all_cases.cc b/all_cases/all_cases.cc index 1a25527..cade9db 100644 --- a/all_cases/all_cases.cc +++ b/all_cases/all_cases.cc @@ -1,9 +1,45 @@ #include "all_cases.h" +#include +#include +#include + std::vector AllCases::basic_ranges; +bool AllCases::basic_ranges_available = false; + +std::mutex AllCases::basic_ranges_building; + void AllCases::build_basic_ranges() { - AllCases::basic_ranges.emplace_back(0); - AllCases::basic_ranges.emplace_back(1); - AllCases::basic_ranges.emplace_back(2); + + std::cout << std::this_thread::get_id() << " enter build function" << std::endl; + + if (AllCases::basic_ranges_available) { + std::cout << std::this_thread::get_id() << " data already built -> skip" << std::endl; + return; // basic ranges already built + } + + if (AllCases::basic_ranges_building.try_lock()) { // lock success -> not building + + std::cout << std::this_thread::get_id() << " try lock success -> start build process" << std::endl; + + AllCases::basic_ranges.emplace_back(0); + AllCases::basic_ranges.emplace_back(1); + AllCases::basic_ranges.emplace_back(2); + sleep(2); // assume using a lot of time + + AllCases::basic_ranges_available = true; // enable available flag + + std::cout << std::this_thread::get_id() << " build complete -> continue" << std::endl; + + } else { // another thread building + + std::cout << std::this_thread::get_id() << " another thread building -> wait" << std::endl; + AllCases::basic_ranges_building.lock(); // blocking wait + + } + std::cout << std::this_thread::get_id() << " work complete -> unlock" << std::endl; + AllCases::basic_ranges_building.unlock(); + + } diff --git a/all_cases/all_cases.h b/all_cases/all_cases.h index 1a79187..ff1e870 100644 --- a/all_cases/all_cases.h +++ b/all_cases/all_cases.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include @@ -9,4 +10,8 @@ public: static void build_basic_ranges(); + static bool basic_ranges_available; + + static std::mutex basic_ranges_building; + }; diff --git a/all_cases/main.cc b/all_cases/main.cc index 5b31114..856ee64 100644 --- a/all_cases/main.cc +++ b/all_cases/main.cc @@ -1,10 +1,26 @@ #include #include "all_cases.h" +#include +#include + int main() { std::cout << AllCases::basic_ranges.size() << std::endl; + + std::thread t1(AllCases::build_basic_ranges); + usleep(100); + std::thread t2(AllCases::build_basic_ranges); + usleep(100); + std::thread t3(AllCases::build_basic_ranges); + usleep(100); + + t1.join(); + t2.join(); + t3.join(); + AllCases::build_basic_ranges(); + std::cout << AllCases::basic_ranges.size() << std::endl; return 0;