Don't write to matches_all simultaneously from different threads.

What did I do yesterday?!? 😬

Closes: #6
This commit is contained in:
tastytea 2021-05-27 20:40:47 +02:00
parent 38bf9be948
commit 8d5565a72c
Signed by: tastytea
GPG Key ID: CFC39497F1B26E07
1 changed files with 8 additions and 3 deletions

View File

@ -33,6 +33,7 @@
#include <future>
#include <iostream>
#include <locale>
#include <mutex>
#include <string>
#include <string_view>
#include <thread>
@ -129,17 +130,21 @@ int main(int argc, char *argv[])
search_settings.context = opts.context;
vector<vector<search::match>> matches_all;
std::mutex mutex_matches_all;
vector<std::future<int>> 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 &regex : opts.regexp)
{
try
{
matches_all.emplace_back(
search::search(filepath, regex, search_settings));
auto matches{
search::search(filepath, regex, search_settings)};
std::lock_guard<std::mutex> guard(mutex_matches_all);
matches_all.emplace_back(matches);
}
catch (const std::exception &e)
{