How to automatically remove completed std::future<void> from a std::vector<std::future<void>>?
I am using futures to execute a messaging system
void RaiseEvent(EventID messageID)
{
mEventExecutors.push_back(std::move(std::async([=]{
auto eventObject = mEventListeners.find(messageID);
if (eventObject != mEventListeners.end())
{
for (auto listener : eventObject->second)
{
listener();
}
}
})
));
}
Is there anyway to automate the removal of the completed futures from mEventExecutors? Or do I need to monitor the state of the futures in the vector and manually erase these?
Posted Jul 15, 2017, 8:14 AM UTC
Side note: Your use of
std::moveis unnecessary.std::moveis for casting an lvalue expression into an rvalue expression that refers to the same object. Here,std::async(...)is already an rvalue expression. In some cases an unecessarystd::moveeven makes things worse as it prevents move elision and lifetime extensions of temporaries.