diff --git a/include/EventManager/Manager.hpp b/include/EventManager/Manager.hpp index 0d4ecc9..e484dee 100644 --- a/include/EventManager/Manager.hpp +++ b/include/EventManager/Manager.hpp @@ -78,14 +78,20 @@ /// the id for the next participant connecting std::uint32_t nextParticipantID_; - /* - * all private methods - */ + /// queue for connection request so connecting is done in manager context + std::queue> connectionQueue_; - /** - * @brief the method running in the main thread - */ - void mainProcess_(); + /// mutex to protect connectionQueue_ + std::mutex mutexConnectionQueue_; + + /* + * all private methods + */ + + /** + * @brief the method running in the main thread + */ + void mainProcess_(); /** * @brief the method running in the scheduling thread @@ -97,6 +103,9 @@ */ void processEvent(const std::shared_ptr event); + + void processConnections_(); + /** * @brief start the main thread */ diff --git a/src/EventManager/Manager.cpp b/src/EventManager/Manager.cpp index 4cfbb38..6eda276 100644 --- a/src/EventManager/Manager.cpp +++ b/src/EventManager/Manager.cpp @@ -198,6 +198,8 @@ } } mutexSchedulingParticipants_.unlock(); + + processConnections_(); std::this_thread::sleep_for(std::chrono::milliseconds(100)); } @@ -341,18 +343,34 @@ } } + void EventManager::Manager::processConnections_() + { + std::lock_guard guard(mutexParticipants_); + std::lock_guard lockGuard(mutexConnectionQueue_); + + while(!connectionQueue_.empty()) + { + std::shared_ptr participant = connectionQueue_.front(); + connectionQueue_.pop(); + particpants_.push_back(participant); + + + participant->init(); + } + } + void EventManager::Manager::connect(std::shared_ptr participant) { std::lock_guard guard(mutexParticipants_); - particpants_.push_back(participant); participant->setManager(shared_from_this()); - // we can set and increment here because only one participant is in this critical - // section in any moment + // we can set and increment here because only one participant is in this + // critical section in any moment participant->setID(nextParticipantID_); nextParticipantID_++; - - participant->init(); + + connectionQueue_.push(participant); + } void EventManager::Manager::disconnect(std::shared_ptr participant)