diff --git a/src/network/NetworkManagerPrimary.cc b/src/network/NetworkManagerPrimary.cc index 0586c8b462..84542b711c 100644 --- a/src/network/NetworkManagerPrimary.cc +++ b/src/network/NetworkManagerPrimary.cc @@ -18,6 +18,7 @@ #include "NetworkManagerPrimary.hh" #include +#include #include #include #include @@ -41,6 +42,7 @@ using namespace ignition; using namespace gazebo; +using namespace std::chrono_literals; ////////////////////////////////////////////////// NetworkManagerPrimary::NetworkManagerPrimary( @@ -142,21 +144,17 @@ bool NetworkManagerPrimary::Step(const UpdateInfo &_info) // Send step to all secondaries this->secondaryStates.clear(); + this->secondaryStatesPromise = std::promise{}; + auto future = this->secondaryStatesPromise.get_future(); this->simStepPub.Publish(step); // Block until all secondaries are done { IGN_PROFILE("Waiting for secondaries"); - int sleep = 0; - int maxSleep = 10 * 1000 * 1000; - for (; sleep < maxSleep && - (this->secondaryStates.size() < this->secondaries.size()); ++sleep) - { - std::this_thread::sleep_for(std::chrono::nanoseconds(1)); - } + auto result = future.wait_for(10s); - if (sleep == maxSleep) + if (std::future_status::ready != result) { ignerr << "Waited 10 s and got only [" << this->secondaryStates.size() << " / " << this->secondaries.size() @@ -202,6 +200,10 @@ std::map void NetworkManagerPrimary::OnStepAck(const msgs::SerializedStateMap &_msg) { this->secondaryStates.push_back(_msg); + if (this->secondaryStates.size() == this->secondaries.size()) + { + this->secondaryStatesPromise.set_value(); + } } ////////////////////////////////////////////////// @@ -316,4 +318,3 @@ void NetworkManagerPrimary::SetAffinity(Entity _performer, auto newAffinity = components::PerformerAffinity(_secondary); this->dataPtr->ecm->CreateComponent(_performer, newAffinity); } - diff --git a/src/network/NetworkManagerPrimary.hh b/src/network/NetworkManagerPrimary.hh index 5bd11e5731..c95fe25e48 100644 --- a/src/network/NetworkManagerPrimary.hh +++ b/src/network/NetworkManagerPrimary.hh @@ -18,6 +18,7 @@ #define IGNITION_GAZEBO_NETWORK_NETWORKMANAGERPRIMARY_HH_ #include +#include #include #include #include @@ -117,6 +118,9 @@ namespace ignition /// \brief Keep track of states received from secondaries. private: std::vector secondaryStates; + + /// \brief Promise used to notify when all secondaryStates where received. + private: std::promise secondaryStatesPromise; }; } } // namespace gazebo