From c7e21669027d12c418f3b0f4fd0e2acd92d65806 Mon Sep 17 00:00:00 2001 From: Tom Smeding Date: Sun, 17 Oct 2021 16:12:59 +0200 Subject: Protect Player results update with mutex --- main.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/main.cpp b/main.cpp index 41ce02a..4adc6bc 100644 --- a/main.cpp +++ b/main.cpp @@ -106,6 +106,7 @@ struct MatchResult { }; struct Player { + mutex lock; string fname, safename; vector results; int64_t lastModified; @@ -113,6 +114,14 @@ struct Player { Player(const string &fname) : fname(fname), safename(makeSafe(fname)), lastModified(fileLastModified(fname)) {} + + // Do not call this in a threaded setting (wouldn't make sense anyway) + Player(Player &&other) + : fname{move(other.fname)} + , safename{move(other.safename)} + , results{move(other.results)} + , lastModified{other.lastModified} + {} }; int MatchResult::score() const { @@ -225,8 +234,14 @@ static void writeMatchCache(const Player &p1, const Player &p2, int index, const } static void recordResult(Player &p1, Player &p2, const MatchResult &result) { - p1.results.push_back(result); - p2.results.push_back(result.inverted()); + { + lock_guard guard{p1.lock}; + p1.results.push_back(result); + } + { + lock_guard guard{p2.lock}; + p2.results.push_back(result.inverted()); + } } static void playMatch(MultiLog &multiLog, Player &p1, Player &p2, int index, const Params ¶ms) { -- cgit v1.2.3-54-g00ecf