mirror of https://github.com/dnomd343/klotski.git
Dnomd343
2 years ago
4 changed files with 64 additions and 67 deletions
@ -1,6 +1,5 @@ |
|||||
cmake_minimum_required(VERSION 3.0) |
cmake_minimum_required(VERSION 3.0) |
||||
set(CMAKE_CXX_STANDARD 14) |
set(CMAKE_CXX_STANDARD 14) |
||||
|
|
||||
#add_executable(klotski main.cc all_cases.cc) |
add_executable(klotski main.cc common.cc basic_ranges.cc all_cases.cc) |
||||
add_executable(klotski main.cc basic_ranges.cc common.cc) |
|
||||
target_link_libraries(klotski pthread) |
target_link_libraries(klotski pthread) |
||||
|
@ -1,45 +1,36 @@ |
|||||
|
#include "common.h" |
||||
#include "all_cases.h" |
#include "all_cases.h" |
||||
|
|
||||
#include <iostream> |
std::mutex AllCases::all_cases_building; |
||||
#include <thread> |
bool AllCases::all_cases_available = false; |
||||
#include <unistd.h> |
std::vector<uint32_t> AllCases::all_cases[]; |
||||
|
|
||||
std::vector<uint32_t> AllCases::basic_ranges; |
const std::vector<uint32_t> (*AllCases::get_all_cases())[16] { // get const ptr of all cases
|
||||
|
if (all_cases->empty()) { |
||||
bool AllCases::basic_ranges_available = false; |
build_all_cases(); // all cases initialize
|
||||
|
|
||||
std::mutex AllCases::basic_ranges_building; |
|
||||
|
|
||||
void AllCases::build_basic_ranges() { |
|
||||
|
|
||||
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
|
|
||||
} |
} |
||||
|
return &all_cases; // return ptr
|
||||
|
} |
||||
|
|
||||
if (AllCases::basic_ranges_building.try_lock()) { // lock success -> not building
|
void AllCases::build_all_cases() { // build all cases
|
||||
|
if (AllCases::all_cases_available) { |
||||
std::cout << std::this_thread::get_id() << " try lock success -> start build process" << std::endl; |
return; // all cases already built
|
||||
|
} |
||||
AllCases::basic_ranges.emplace_back(0); |
if (AllCases::all_cases_building.try_lock()) { // lock success -> start building
|
||||
AllCases::basic_ranges.emplace_back(1); |
/// head -> 0/1/2 / 4/5/6 / 8/9/10 / 12/13/14
|
||||
AllCases::basic_ranges.emplace_back(2); |
for (uint32_t head = 0; head < 16; ++head) { // address of 2x2 block
|
||||
sleep(2); // assume using a lot of time
|
if ((head & 0b11) == 0b11) { |
||||
|
continue; // invalid 2x2 address
|
||||
AllCases::basic_ranges_available = true; // enable available flag
|
} |
||||
|
for (uint32_t const &range : *get_basic_ranges()) { // check base on 2x2 address and range
|
||||
std::cout << std::this_thread::get_id() << " build complete -> continue" << std::endl; |
if (Common::check_case(head, range)) { |
||||
|
all_cases[head].emplace_back(Common::range_reverse(range)); // found valid case
|
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
AllCases::all_cases_available = true; // set available flag
|
||||
} else { // another thread building
|
} else { // another thread building
|
||||
|
AllCases::all_cases_building.lock(); // blocking waiting
|
||||
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::all_cases_building.unlock(); |
||||
AllCases::basic_ranges_building.unlock(); |
|
||||
|
|
||||
|
|
||||
} |
} |
||||
|
Loading…
Reference in new issue