From f083fcc2be0e0d3b6e356f0a814f407a273e345a Mon Sep 17 00:00:00 2001 From: lonkaars Date: Wed, 14 Dec 2022 10:31:39 +0100 Subject: fix unsafe use of mutex lock --- oop2w5/Blackboard.cpp | 15 ++++++++------- 1 file 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* bbs_lock = new lock_guard(_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 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])()); } -- cgit v1.2.3