Bil*_*ias 9 c++ iteration dynamic
我正在迭代一组回调函数.函数在迭代期间被调用,并且可能导致对函数集的实际容器的急剧改变.
我现在在做的是:
检查每个元素的存在是超级动态的,但似乎也很慢.
还有其他建议来解决这个案子吗?
编辑:这是实际代码:
// => 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);
}
};
Run Code Online (Sandbox Code Playgroud)
我想到了两种基本方法:
使用基于任务的方法(在集合锁定的情况下,将任务推送到每个元素的队列中,然后释放所有各方去做工作并等待完成)。当任务实际启动时,您仍然需要检查当前任务的元素是否仍然存在/当前在集合中。
使用并发数据结构(我的意思是,一种适合多线程访问而无需显式锁定的数据结构)。以下库包含并发数据结构的实现:
(稍后添加链接)