diff options
author | lonkaars <loek@pipeframe.xyz> | 2022-12-14 10:31:39 +0100 |
---|---|---|
committer | lonkaars <loek@pipeframe.xyz> | 2022-12-14 10:31:39 +0100 |
commit | f083fcc2be0e0d3b6e356f0a814f407a273e345a (patch) | |
tree | 36be6d92935f88d3d7e83697671dbefc6d92fc51 | |
parent | 1ed8db0234f9a273e07b7350e53ad230c5b62ce6 (diff) |
fix unsafe use of mutex lock
-rw-r--r-- | oop2w5/Blackboard.cpp | 15 |
1 files changed, 8 insertions, 7 deletions
diff --git a/oop2w5/Blackboard.cpp b/oop2w5/Blackboard.cpp index 9c98992..26ced40 100644 --- a/oop2w5/Blackboard.cpp +++ b/oop2w5/Blackboard.cpp @@ -35,14 +35,15 @@ void Blackboard::spawn_subthreads(unsigned start_index, unsigned length) { // beslis beste strategie (lelijk linear zoeken want maar 3 elementen) - lock_guard<mutex>* bbs_lock = new lock_guard<mutex>(_bbs_lock); // lock guard - _bbs->setBestStrategy(_fitness[start_index]); // beste fitness altijd in _fitness - for (unsigned i = start_index; i < end_index; i++) - _bbs->setBestStrategy(max(_fitness[i], _bbs->getBestStrategy())); unsigned best_index = start_index; - for (unsigned i = start_index; i < end_index; i++) - if (_fitness[i] == _bbs->getBestStrategy()) { best_index = i; break; } - delete bbs_lock; // handmatig mutex vrijgeven (want volgende regel code duurt lang) + { + lock_guard<mutex> bbs_lock (_bbs_lock); // lock guard + _bbs->setBestStrategy(_fitness[start_index]); // beste fitness altijd in _fitness + for (unsigned i = start_index; i < end_index; i++) + _bbs->setBestStrategy(max(_fitness[i], _bbs->getBestStrategy())); + for (unsigned i = start_index; i < end_index; i++) + if (_fitness[i] == _bbs->getBestStrategy()) { best_index = i; break; } + } _results.push_back((_bbs->*resultStrategies[best_index])()); } |