Proper use of Threads.Condition

Hi! I’m trying to implement thread-safe event listeners, something that reminds me of Java Event listeners.

The idea is that I have many parts of a system that must react to change events in another part of the system. So a listener will wait until an event happens, handle the event and then wait for the next event.

The solution I’m giving for this problem is this code at:
https://gist.github.com/felipenoris/10e135bf6dcb0298057762146b4104e2

I would like feedback if I’m doing something wrong, or if there is a better way to code this.

Thanks in advance!

1 Like

Nice! I did something very hacky in my satellite simulator to listen to the events that come from the GUI. It will be nice to have something more polished.

You could make use of the following idiom:

lock(condition) do
    ...
    wait(condition) # or notify(condidtion)
    ...
end

This uses the try ... finally statement internally: Base.lock source code

2 Likes