From 7c0d4e2a579debd375b132efb124742ca6720f80 Mon Sep 17 00:00:00 2001 From: eriedaberrie Date: Mon, 8 Jan 2024 21:56:16 -0800 Subject: [PATCH] Only lock once per iteration --- src/main.cpp | 25 ++++++++++--------------- 1 file changed, 10 insertions(+), 15 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index dba5f23..137b5e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -183,18 +183,12 @@ int main(int argc, char *argv[]) { for (unsigned int w = 1; w <= wc; w++) { workers.push_back(std::thread([&it, &n_done, &it_lock, &all_fires, &residentials, &out_dir, w, distance]() { - for (;;) { - it_lock.lock(); - if (it == all_fires.end()) { - std::cout << "Worker " << w << " done! (finished " << n_done - << "/" << all_fires.size() << ")" << std::endl; - it_lock.unlock(); - return; - } + it_lock.lock(); + while (it != all_fires.end()) { const auto& month_data = *(it++); - auto& month = month_data.name; - auto& month_fires = month_data.days; + const auto& month = month_data.name; + const auto& month_fires = month_data.days; std::cout << "Worker " << w << " processing data from " << month << "... (finished " << n_done << "/" << all_fires.size() << ")" << std::endl; @@ -225,12 +219,13 @@ int main(int argc, char *argv[]) { outf << '\n'; } - { - std::lock_guard lk(it_lock); - - n_done++; - } + it_lock.lock(); + n_done++; } + + std::cout << "Worker " << w << " done! (finished " << n_done + << "/" << all_fires.size() << ")" << std::endl; + it_lock.unlock(); })); }