I am iterating over a set of callback functions. Functions are called during iteration and may lead to drastic changes to the actual container of the functions set.
What I am doing now is:
- make a copy of original set
- iterate over copy, but for every element check whether it still exists in the original set
Checking for every element's existence is super-dynamic, but seems quite slow too.
Are there other propositions to tackle this case?
Edit : here is the actual code :
// => i = event id template <class Param> void dispatchEvent(int i, Param param) { EventReceiverSet processingNow; const EventReceiverSet& eventReceiverSet = eventReceiverSets[i]; std::copy(eventReceiverSet.begin(), eventReceiverSet.end(), std::inserter(processingNow, processingNow.begin())); while (!processingNow.empty()) { EventReceiverSet::iterator it = processingNow.begin(); IFunction<>* function = it->getIFunction(); /// get function before removing iterator processingNow.erase(it); // is EventReceiver still valid? (may have been removed from original set) if (eventReceiverSet.find(ERWrapper(function)) == eventReceiverSet.end()) continue; // not found function->call(param); } };