Concurrent resource access in event callbacks
rhythmize opened this issue · 4 comments
Hi,
If I have a common object shared between multiple event listeners. Do I need to take care of read/ write access of that object in the critical section myself, Or event callbacks are triggered in a sequential fashion only?
Assuming all my listeners are in the same thread.
P.S.: I'm using master
branch as of now.
Hello
Current version on master is also single threaded. Whenever you call notify
it will be executed on the same thread that you call notify
. Listeners aren't tied to any thread.
Yes callbacks are triggered in sequential fashion.
On master -> AsyncEventBus thanks to this one you can process events on other threads but remember that listeners will be executed also on that thread.
On develop branch everything behaves as AsyncEventBus. I mean you decide on which thread you want to consume events.
So, ideally, I'll need to add mutex on the shared resource only when my listeners exists in different threads. If they are in the same thread, it should be fine without mutex as well.
Please correct if I misunderstood something.
Nope, it only depends on where you call 'notify' example:
Thread # 1
{
bus.notify(Event1) -> call listener_1, call listener_2
}
Thread # 2
{
bus.notify(Event2) -> call listener_3, call listener_4
}
So it only depends on which thread you call notify
As you can see in code calling notify will immediately walk through listeners.
If you want to split notify and processing of events you need to use AsyncEventBus, EventBus implementation is like: schedule + consume event
AsyncEventBys is like: postpone event, later call when you need process
So what I can suggest is to use EventBus for single thread and if you want to achieve cross communication between threads use AsyncEventBus with schedule
+ consume
Okay, Thanks.