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();