I was executing it in a update function, which is a ordinary Unity function. The hashset monofuncCBSet will be modified somewhere,but I dont think it is a multithread. I am cofused about the reason…
you cannot modify a collection’s content while it is being enumerated. That includes adding or removing items from the collection (e.g by deleting/destroying items).
I was modifying this hashset somewhere else but not in this Update function. In consideration of no Multi-threading involved, I dont think I have changed the amount of the hashset.Could it be said that the MonoUpdate function was the real reason that have changed the content itself…
I tried to use list ,it worked, but for efficiency,it is not the best choice.Cause the hashset would be inserted or removed frequently. Is there some way I can ergodic the collection with high efficiency?
mono.MonoUpdate() is probably the thing that is changing monoFuncCBSet;
Use two different buffers to insert / remove upon insert / removal + monoFuncCBSet.
E.g.
Upon insert → add to the insert buffer (different hashset) and remove from the removal buffer;
Upon removal → add to the removal buffer (different hashset) and remove from the insertion buffer;
Upon update:
3.1. Foreach on the insert buffer → add items to the monoFuncCBSet;
3.2. Foreach on the removal buffer → remove items from the monoFuncCBSet;
3.3. Clear buffers above;
3.4. Perform foreach on monoFuncCBSet.
This should allow you to modify monoFuncCBSet in a safe way, as well as modify update manager entries more than one time per frame safely.
Alternatively, you can use lists for the buffers, but I’ve found that its harder to remove from it / more ops than hashset.
I’m assuming you’re writing your own update manager, so this is what I’ve done for my own manager.