czyzby/gdx-lml

[Autumn] Multithreaded access to EventDispatcher

Velaskes11 opened this issue · 1 comments

I use EventDispatcher.postEvent in several threads.
Some times I get exception:

Fatal Exception: com.badlogic.gdx.utils.j
#iterator() cannot be used nested.

com.badlogic.gdx.utils.ObjectSet$ObjectSetIterator.hasNext (SourceFile:533)
com.github.czyzby.autumn.processor.event.EventDispatcher.invokeEventListeners (SourceFile:132)
com.github.czyzby.autumn.processor.event.EventDispatcher.postEvent (SourceFile:121)
com.a.b.c$$Lambda$0.run (Unknown Source:1099)
java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
java.util.concurrent.ThreadPoolExecutor$Worker.run (ThreadPoolExecutor.java:607)
java.lang.Thread.run (Thread.java:761)

It is correct to use it from multiple threads?
It seems that need synchronization in method invokeEventListeners or something like this.

Well, if the event listeners are thread-safe/stateless and they collection never changes, this should not cause any issues - iteration over effectively immutable list of listeners should not cause any problems. The exception is thrown because LibGDX ObjectSet reuses its iterator and this causes issues when used from within multiple threads. I'll look into this.