summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Smeding <tom@tomsmeding.com>2021-10-17 16:12:59 +0200
committerTom Smeding <tom@tomsmeding.com>2021-10-17 16:12:59 +0200
commitc7e21669027d12c418f3b0f4fd0e2acd92d65806 (patch)
treea98333e946fa0d2d1d63e7dab21360a83630e04c
parente6c6502b8e408c5a0c62bd0fa30f958fedc253f6 (diff)
Protect Player results update with mutex
-rw-r--r--main.cpp19
1 files 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<MatchResult> 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 &params) {