diff --git a/Chromosome.cpp b/Chromosome.cpp index dc6c3c1..c6b7761 100644 --- a/Chromosome.cpp +++ b/Chromosome.cpp @@ -10,6 +10,7 @@ #include #include #include +#include // @@ -42,6 +43,7 @@ namespace BitEvolver void Chromosome::Randomize() { // + std::unique_lock lock(this->modification_mutex); int i; // @@ -56,6 +58,9 @@ namespace BitEvolver // void Chromosome::SetBitCount(int count) { + // + std::unique_lock lock(this->modification_mutex); + // this->bits_count_desired = count; this->Randomize(); @@ -70,6 +75,9 @@ namespace BitEvolver // void Chromosome::FlipBit(int index) { + // + std::unique_lock lock(this->modification_mutex); + // if ( index >= (int)this->bits.size() ) { throw std::runtime_error("Chromosome::FlipBit() - Tried to flip out of range bit index: " + to_string(index)); @@ -87,6 +95,9 @@ namespace BitEvolver // bool Chromosome::GetBit(int index) { + // + std::unique_lock lock(this->modification_mutex); + // if ( index >= (int)this->bits.size() ) { throw std::runtime_error("Chromosome::GetBit() - Tried to access out of bounds bit"); @@ -99,6 +110,9 @@ namespace BitEvolver // void Chromosome::SetBit(int index, bool b) { + // + std::unique_lock lock(this->modification_mutex); + // if ( index >= (int)this->bits.size() ) { throw std::runtime_error("Chromosome::GetBit() - Tried to access out of bounds bit"); @@ -168,6 +182,7 @@ namespace BitEvolver string Chromosome::ToString() { // + std::unique_lock lock(this->modification_mutex); stringstream s; // @@ -190,6 +205,7 @@ namespace BitEvolver const Chromosome& Chromosome::operator=(const Chromosome& other) { // + std::unique_lock lock1(this->modification_mutex); int i; // diff --git a/Chromosome.h b/Chromosome.h index 99ad214..b07eb37 100644 --- a/Chromosome.h +++ b/Chromosome.h @@ -13,6 +13,7 @@ #include #include #include +#include // @@ -74,6 +75,9 @@ namespace BitEvolver // Fitness double fitness; + + // Mutexes + std::recursive_mutex modification_mutex; }; };