Modification mutexes

This commit is contained in:
Mike 2018-04-14 02:23:29 -07:00
parent 0326bc4ddf
commit 35fa952da6
2 changed files with 20 additions and 0 deletions

View File

@ -10,6 +10,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <mutex>
// //
@ -42,6 +43,7 @@ namespace BitEvolver
void Chromosome::Randomize() void Chromosome::Randomize()
{ {
// //
std::unique_lock<std::recursive_mutex> lock(this->modification_mutex);
int i; int i;
// //
@ -56,6 +58,9 @@ namespace BitEvolver
// //
void Chromosome::SetBitCount(int count) void Chromosome::SetBitCount(int count)
{ {
//
std::unique_lock<std::recursive_mutex> lock(this->modification_mutex);
// //
this->bits_count_desired = count; this->bits_count_desired = count;
this->Randomize(); this->Randomize();
@ -70,6 +75,9 @@ namespace BitEvolver
// //
void Chromosome::FlipBit(int index) void Chromosome::FlipBit(int index)
{ {
//
std::unique_lock<std::recursive_mutex> lock(this->modification_mutex);
// //
if ( index >= (int)this->bits.size() ) { if ( index >= (int)this->bits.size() ) {
throw std::runtime_error("Chromosome::FlipBit() - Tried to flip out of range bit index: " + to_string(index)); 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) bool Chromosome::GetBit(int index)
{ {
//
std::unique_lock<std::recursive_mutex> lock(this->modification_mutex);
// //
if ( index >= (int)this->bits.size() ) { if ( index >= (int)this->bits.size() ) {
throw std::runtime_error("Chromosome::GetBit() - Tried to access out of bounds bit"); 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) void Chromosome::SetBit(int index, bool b)
{ {
//
std::unique_lock<std::recursive_mutex> lock(this->modification_mutex);
// //
if ( index >= (int)this->bits.size() ) { if ( index >= (int)this->bits.size() ) {
throw std::runtime_error("Chromosome::GetBit() - Tried to access out of bounds bit"); throw std::runtime_error("Chromosome::GetBit() - Tried to access out of bounds bit");
@ -168,6 +182,7 @@ namespace BitEvolver
string Chromosome::ToString() string Chromosome::ToString()
{ {
// //
std::unique_lock<std::recursive_mutex> lock(this->modification_mutex);
stringstream s; stringstream s;
// //
@ -190,6 +205,7 @@ namespace BitEvolver
const Chromosome& Chromosome::operator=(const Chromosome& other) const Chromosome& Chromosome::operator=(const Chromosome& other)
{ {
// //
std::unique_lock<std::recursive_mutex> lock1(this->modification_mutex);
int i; int i;
// //

View File

@ -13,6 +13,7 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <vector> #include <vector>
#include <mutex>
// //
@ -74,6 +75,9 @@ namespace BitEvolver
// Fitness // Fitness
double fitness; double fitness;
// Mutexes
std::recursive_mutex modification_mutex;
}; };
}; };