diff --git a/src/ServerPrivate.cc b/src/ServerPrivate.cc index f38dd9d025..a4abb8f3a4 100644 --- a/src/ServerPrivate.cc +++ b/src/ServerPrivate.cc @@ -168,16 +168,22 @@ bool ServerPrivate::Run(const uint64_t _iterations, } else { + if (!this->workerPool.has_value()) + { + // Initialize the workerpool if we do have multiple simulation runners and + // it hasn't been initialized before + this->workerPool.emplace(2); + } for (std::unique_ptr &runner : this->simRunners) { - this->workerPool.AddWork([&runner, &_iterations] () + this->workerPool->AddWork([&runner, &_iterations] () { runner->Run(_iterations); }); } // Wait for the runner to complete. - result = this->workerPool.WaitForResults(); + result = this->workerPool->WaitForResults(); } this->running = false; diff --git a/src/ServerPrivate.hh b/src/ServerPrivate.hh index a41640262f..4680410f9b 100644 --- a/src/ServerPrivate.hh +++ b/src/ServerPrivate.hh @@ -147,7 +147,10 @@ namespace ignition const gz::msgs::ServerControl &_req, msgs::Boolean &_res); /// \brief A pool of worker threads. - public: common::WorkerPool workerPool{2}; + /// \note We use optional here since most of the time, there will be a + /// single simulation runner and a workerpool is not needed. We will + /// initialize the workerpool as necessary later on. + public: std::optional workerPool; /// \brief All the simulation runners. public: std::vector> simRunners; diff --git a/src/SimulationRunner.hh b/src/SimulationRunner.hh index aec30c3296..0d6ed516cd 100644 --- a/src/SimulationRunner.hh +++ b/src/SimulationRunner.hh @@ -37,7 +37,6 @@ #include #include -#include #include #include #include @@ -410,9 +409,6 @@ namespace ignition /// \brief Manager of distributing/receiving network work. private: std::unique_ptr networkMgr{nullptr}; - /// \brief A pool of worker threads. - private: common::WorkerPool workerPool{2}; - /// \brief Wall time of the previous update. private: std::chrono::steady_clock::time_point prevUpdateRealTime;