Commit 2d1fb525 authored by Mohcine Chraibi's avatar Mohcine Chraibi

Main thread waits until source thread starts

parent e34ec347
Pipeline #9288 failed with stages
in 4 minutes and 34 seconds
...@@ -87,8 +87,11 @@ int main(int argc, char** argv) ...@@ -87,8 +87,11 @@ int main(int argc, char** argv)
//std::thread t1(sim.GetAgentSrcManager()); //std::thread t1(sim.GetAgentSrcManager());
double simMaxTime = configuration->GetTmax(); double simMaxTime = configuration->GetTmax();
std::thread t1(&AgentsSourcesManager::Run, &sim.GetAgentSrcManager()); std::thread t1(&AgentsSourcesManager::Run, &sim.GetAgentSrcManager());
std::this_thread::sleep_for(std::chrono::milliseconds(100)); while(!sim.GetAgentSrcManager().IsRunning())
//main thread for the simulation {
// std::cout << "waiting...\n";
}
//main thread for the simulation
evacTime = sim.RunStandardSimulation(simMaxTime); evacTime = sim.RunStandardSimulation(simMaxTime);
//Join the main thread //Join the main thread
t1.join(); t1.join();
...@@ -112,7 +115,6 @@ int main(int argc, char** argv) ...@@ -112,7 +115,6 @@ int main(int argc, char** argv)
} }
double execTime = difftime(endtime, starttime); double execTime = difftime(endtime, starttime);
std::stringstream summary; std::stringstream summary;
summary << std::setprecision(2) << std::fixed; summary << std::setprecision(2) << std::fixed;
summary << "\nExec Time [s] : " << execTime << std::endl; summary << "\nExec Time [s] : " << execTime << std::endl;
......
...@@ -55,13 +55,15 @@ void AgentsSourcesManager::operator()() ...@@ -55,13 +55,15 @@ void AgentsSourcesManager::operator()()
void AgentsSourcesManager::Run() void AgentsSourcesManager::Run()
{ {
SetRunning(true);
Log->Write("INFO:\tStarting agent manager thread"); Log->Write("INFO:\tStarting agent manager thread");
/* std::cout<< KGRN << "\n Starting agent manager thread\n" << "\n>> time: " << Pedestrian::GetGlobalTime() << RESET << "\n"; */
//Generate all agents required for the complete simulation //Generate all agents required for the complete simulation
//It might be more efficient to generate at each frequency step //It might be more efficient to generate at each frequency step
//TODO this loop is exactly GenerateAgents( --> REFACTOR) //TODO this loop is exactly GenerateAgents( --> REFACTOR)
for (const auto& src : _sources) for (const auto& src : _sources)
{ {
/* std::cout << "Generate AgentsAndAddToPool src: " << src->GetId() << "\n" ; */
src->GenerateAgentsAndAddToPool(src->GetMaxAgents(), _building); src->GenerateAgentsAndAddToPool(src->GetMaxAgents(), _building);
} }
...@@ -78,13 +80,16 @@ void AgentsSourcesManager::Run() ...@@ -78,13 +80,16 @@ void AgentsSourcesManager::Run()
do do
{ {
int current_time = (int)Pedestrian::GetGlobalTime(); int current_time = (int)Pedestrian::GetGlobalTime();
/* std::cout << KBLU << ">> RUN: current_time " << current_time << " last update " << _lastUpdateTime << "\n" << RESET; */
if ((current_time != _lastUpdateTime) if ((current_time != _lastUpdateTime)
&& ((current_time % updateFrequency) == 0)) && ((current_time % updateFrequency) == 0))
{ {
/* std::cout << ">> RUN: current_time " << current_time << " last update " << _lastUpdateTime << std::endl; */ /* std::cout << " --- Enter IF --- \n" << KRED << "QUEUE isempty: " << AgentsQueueIn::IsEmpty() << "\n" << RESET; */
if(AgentsQueueIn::IsEmpty()) if(AgentsQueueIn::IsEmpty())
//if queue is empty. Otherwise, wait for main thread to empty it and update _building //if queue is empty. Otherwise, wait for main thread to empty it and update _building
{ {
/* std::cout << " --- Enter QUEUE EMPTY --- \n"; */
finished=ProcessAllSources(); finished=ProcessAllSources();
_lastUpdateTime = current_time; _lastUpdateTime = current_time;
//SetBuildingUpdated(false); //SetBuildingUpdated(false);
...@@ -95,22 +100,20 @@ void AgentsSourcesManager::Run() ...@@ -95,22 +100,20 @@ void AgentsSourcesManager::Run()
break; // break if max simulation time is reached. break; // break if max simulation time is reached.
} while (!finished); } while (!finished);
std::cout << ">> Terminating agent manager thread" << RESET << std::endl;
Log->Write("INFO:\tTerminating agent manager thread"); Log->Write("INFO:\tTerminating agent manager thread");
_isCompleted = true; _isCompleted = true;
} }
bool AgentsSourcesManager::ProcessAllSources() const bool AgentsSourcesManager::ProcessAllSources() const
{ {
std::cout << "\nSTART AgentsSourcesManager::ProcessAllSources()\n"; /* std::cout << "\nSTART AgentsSourcesManager::ProcessAllSources()\n"; */
bool empty=true; bool empty=true;
double current_time = Pedestrian::GetGlobalTime(); double current_time = Pedestrian::GetGlobalTime();
vector<Pedestrian*> source_peds; // we have to collect peds from all sources, so that we can consider them while computing new positions vector<Pedestrian*> source_peds; // we have to collect peds from all sources, so that we can consider them while computing new positions
for (const auto& src : _sources) for (const auto& src : _sources)
{ {
/* std::cout << KRED << "\nprocessing src: " << src->GetId() << " -- current time: " << current_time << " schedule: " << src->GetPlanTime() <<". number of peds in building " << _building->GetAllPedestrians().size() << "\n" << RESET; */ /* std::cout << KRED << "\nprocessing src: " << src->GetId() << " -- current time: " << current_time << " schedule time: " << src->GetPlanTime() <<". number of peds in building " << _building->GetAllPedestrians().size() << "\n" << RESET; */
if (src->GetPoolSize() && (src->GetPlanTime() <= current_time) )// maybe diff<eps if (src->GetPoolSize() && (src->GetPlanTime() <= current_time) )// maybe diff<eps
{ {
...@@ -602,6 +605,11 @@ bool AgentsSourcesManager::IsCompleted() const ...@@ -602,6 +605,11 @@ bool AgentsSourcesManager::IsCompleted() const
return _isCompleted; return _isCompleted;
} }
bool AgentsSourcesManager::IsRunning() const
{
return _isRunning;
}
bool AgentsSourcesManager::IsBuildingUpdated() const bool AgentsSourcesManager::IsBuildingUpdated() const
{ {
...@@ -613,6 +621,12 @@ void AgentsSourcesManager::SetBuildingUpdated(bool update) ...@@ -613,6 +621,12 @@ void AgentsSourcesManager::SetBuildingUpdated(bool update)
_buildingUpdated = update; _buildingUpdated = update;
} }
void AgentsSourcesManager::SetRunning(bool running)
{
_isRunning = running;
}
Building* AgentsSourcesManager::GetBuilding() const Building* AgentsSourcesManager::GetBuilding() const
{ {
return _building; return _building;
......
...@@ -84,6 +84,7 @@ public: ...@@ -84,6 +84,7 @@ public:
* Set the building object * Set the building object
*/ */
void SetBuilding(Building* building); void SetBuilding(Building* building);
void SetRunning(bool running);
/** /**
* @return true if all agents have been generated * @return true if all agents have been generated
...@@ -125,7 +126,7 @@ public: ...@@ -125,7 +126,7 @@ public:
int GetMaxSimTime() const; int GetMaxSimTime() const;
void SetMaxSimTime(int t); void SetMaxSimTime(int t);
bool IsRunning() const;
private: private:
/** /**
...@@ -184,6 +185,8 @@ private: ...@@ -184,6 +185,8 @@ private:
//std::atomic<bool>_isCompleted=false; //std::atomic<bool>_isCompleted=false;
// std::atomic<bool>_buildingUpdated=false; // std::atomic<bool>_buildingUpdated=false;
bool _buildingUpdated; bool _buildingUpdated;
bool _isRunning = false;
}; };
#endif /* AGENTSSOURCESMANAGER_H_ */ #endif /* AGENTSSOURCESMANAGER_H_ */
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment