#include #include "Blackboard.h" using std::max; using std::lock_guard; Blackboard::Blackboard() { _bbs = new BlackboardStrategy(); } Blackboard::~Blackboard() { delete _bbs; } void Blackboard::run() { _subthreads[0] = new thread([this] { spawn_subthreads(0, 3); }); // 1, 2, 3 _subthreads[1] = new thread([this] { spawn_subthreads(3, 3); }); // 4, 5, 6 _subthreads[0]->join(); _subthreads[1]->join(); _bbs->setBestResult(max(_results[0], _results[1])); } void Blackboard::spawn_subthreads(unsigned start_index, unsigned length) { unsigned end_index = start_index + length; // bereken geschiktheid strategie for (unsigned i = start_index; i < end_index; i++) _subsubthreads[i] = new thread([this, i] { _fitness[i] = (_bbs->*fitnessStrategies[i])(); }); // join alles for (unsigned i = start_index; i < end_index; i++) _subsubthreads[i]->join(); // beslis beste strategie (lelijk linear zoeken want maar 3 elementen) unsigned best_index = start_index; { 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])()); }