Nonogram solver
Revision | 7caf5c8ad43e7ff8a2d108b9b66698d332be2702 (tree) |
---|---|
Time | 2021-03-06 14:38:45 |
Author | Alexander Larin <scalar438@gmai...> |
Commiter | Alexander Larin |
Add items counter
@@ -1,10 +1,12 @@ | ||
1 | 1 | #include "row_solver_bf.hpp" |
2 | 2 | #include <cell.hpp> |
3 | +#include <row_solver.hpp> | |
4 | + | |
5 | +#include <chrono> | |
3 | 6 | #include <iostream> |
4 | 7 | #include <memory> |
5 | 8 | #include <mutex> |
6 | 9 | #include <optional> |
7 | -#include <row_solver.hpp> | |
8 | 10 | #include <string_view> |
9 | 11 | #include <thread> |
10 | 12 | #include <vector> |
@@ -112,15 +114,14 @@ | ||
112 | 114 | return res; |
113 | 115 | } |
114 | 116 | |
115 | -void run_in_one_thread(std::shared_ptr<std::mutex> mtx, | |
116 | - vector<pair<vector<Block> *, string *>> &data, | |
117 | +void run_in_one_thread(std::mutex &mtx, vector<pair<vector<Block> *, string *>> &data, | |
117 | 118 | optional<tuple<string, string, string>> &answer_mismatch) |
118 | 119 | { |
119 | 120 | while (1) |
120 | 121 | { |
121 | 122 | pair<vector<Block> *, string *> one_run_data; |
122 | 123 | { |
123 | - std::lock_guard g(*mtx); | |
124 | + std::lock_guard g(mtx); | |
124 | 125 | if (data.empty()) break; |
125 | 126 | one_run_data = data.back(); |
126 | 127 | data.pop_back(); |
@@ -128,16 +129,28 @@ | ||
128 | 129 | auto res = solve(*one_run_data.second, *one_run_data.first); |
129 | 130 | if (res.first != res.second) |
130 | 131 | { |
131 | - std::lock_guard g(*mtx); | |
132 | + std::lock_guard g(mtx); | |
132 | 133 | data.clear(); |
133 | 134 | answer_mismatch = make_tuple(*one_run_data.second, res.first, res.second); |
134 | 135 | } |
135 | 136 | } |
136 | 137 | } |
137 | 138 | |
139 | +void items_counter(std::mutex &mtx, vector<pair<vector<Block> *, string *>> &input_data) | |
140 | +{ | |
141 | + while (1) | |
142 | + { | |
143 | + this_thread::sleep_for(std::chrono::seconds(1)); | |
144 | + lock_guard g(mtx); | |
145 | + auto s = input_data.size(); | |
146 | + if (s == 0) break; | |
147 | + cout << "Items remaining: " << s << '\n'; | |
148 | + } | |
149 | +} | |
150 | + | |
138 | 151 | int main() |
139 | 152 | { |
140 | - const int n = 3; | |
153 | + const int n = 7; | |
141 | 154 | auto all_blocks = gen_all_blocks(n); |
142 | 155 | auto all_rows = gen_all_rows(n); |
143 | 156 | vector<pair<vector<Block> *, string *>> all_input; |
@@ -149,12 +162,12 @@ | ||
149 | 162 | } |
150 | 163 | } |
151 | 164 | vector<thread> v_thrd; |
152 | - auto mtx = std::make_shared<std::mutex>(); | |
165 | + std::mutex mtx; | |
153 | 166 | optional<tuple<string, string, string>> answer_mismatch; |
154 | 167 | for (unsigned int i = 0; i != std::thread::hardware_concurrency(); ++i) |
155 | - v_thrd.emplace_back( | |
156 | - thread(run_in_one_thread, mtx, std::ref(all_input), std::ref(answer_mismatch))); | |
157 | - | |
168 | + v_thrd.emplace_back(thread(run_in_one_thread, std::ref(mtx), std::ref(all_input), | |
169 | + std::ref(answer_mismatch))); | |
170 | + v_thrd.push_back(thread(items_counter, ref(mtx), ref(all_input))); | |
158 | 171 | for (auto &thrd : v_thrd) |
159 | 172 | thrd.join(); |
160 | 173 | if (answer_mismatch) |