From 5619ced265a522b6b480ec7f594875aa14b3ceb1 Mon Sep 17 00:00:00 2001 From: rektdie Date: Thu, 3 Apr 2025 03:42:33 +0200 Subject: [PATCH] added multithreading to datagen --- source/datagen.cpp | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/source/datagen.cpp b/source/datagen.cpp index e5265bc..c3334d3 100644 --- a/source/datagen.cpp +++ b/source/datagen.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include "datagen.h" #include "movegen.h" #include "evaluate.h" @@ -43,6 +44,8 @@ static void ShowCursorLinux() { namespace DATAGEN { +std::mutex mutex; + static void PlayRandMoves(Board &board) { std::random_device dev; std::mt19937 rng(dev()); @@ -125,6 +128,7 @@ std::istream &operator>>(std::istream &is, Game &game) { } static void WriteToFile(const Game &game, const std::string &filename) { + std::lock_guard lock(mutex); // Create a buffer to store the serialized data [prevents corruption] std::vector buffer; @@ -278,8 +282,10 @@ void Run(int targetPositions, int threads) { std::atomic positions = 0; - auto worker = std::thread(PlayGame, std::ref(positions), targetPositions * 1000); - worker.detach(); + for (int i = 0; i < threads; i++) { + auto worker = std::thread(PlayGame, std::ref(positions), targetPositions * 1000); + worker.detach(); + } int lastPrintedPosition = 0; double lastPrintedTime = stopwatch.GetElapsedSec();