From 8d5565a72c750ec70ff5e72e8e87f37331c72301 Mon Sep 17 00:00:00 2001 From: tastytea Date: Thu, 27 May 2021 20:40:47 +0200 Subject: [PATCH] Don't write to matches_all simultaneously from different threads. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit What did I do yesterday?!? 😬 Closes: https://schlomp.space/tastytea/epubgrep/issues/6 --- src/main.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index b59fea8..adb8bcf 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -129,17 +130,21 @@ int main(int argc, char *argv[]) search_settings.context = opts.context; vector> matches_all; + std::mutex mutex_matches_all; vector> futurepool; auto search_file{ - [&opts, &matches_all, &search_settings](fs::path filepath) + [&opts, &matches_all, &mutex_matches_all, + &search_settings](const fs::path &filepath) { for (const auto ®ex : opts.regexp) { try { - matches_all.emplace_back( - search::search(filepath, regex, search_settings)); + auto matches{ + search::search(filepath, regex, search_settings)}; + std::lock_guard guard(mutex_matches_all); + matches_all.emplace_back(matches); } catch (const std::exception &e) {