Are ofEvents thread safe?


It’s kind of a basic question, so I was surprised I couldn’t find any info in the docs or the forum.

What’s the thread safety of using OF’s event system? Is it legal and tested to throw events from worker threads? If I do it, which thread will run the job? The same worker thread or the UI thread?

Thanks in advance,


ofEvents are thread safe, but the listeners will process the events in the child thread.

So, if you have a multi-threaded task management system that emits events when a job is complete back into a callback in your main ofApp, those callbacks will be executed by the child thread, not the main thread.

The main issue here is data safety. To protect the data in these cases you probably need a mutex (e.g. ofMutex) unless you have set up your program to prevent data races.

ofEvents simply extend Poco::BasicEvent so if you want to learn more, you can check out this slide show:

If thread-safe worker task management is what you’re trying to accomplish, I recently wrote It uses a combination of Poco’s Notification center and Poco::Task to ensure that events emitted by the worker threads are only dispatched during the application’s main thread (i.e. in the ofApp::update() loop for example). This prevents the need for mutexes and allows somewhat easier cross thread loading of image assets, etc which require GL calls to happen in the main thread.

Good luck!


Just to clarify … ofEvents are thread safe, but the data you pass with the event is not automatically :slight_smile:

@bakercp i’ve noticed this before, and it causes occasional runtime errors. what i do instead is set some status bool to true and process the data in the next frame or the next time the class’s update function is called, but there must be a more–let’s say professional–way of doing this, no?

Thanks @bakercp, that’s really helpful stuff.

@genekogan Check out ofxTaskQueue for a more “professional” way using a Poco::NotificationQueue :smile:

1 Like

in the latest version in master there’s a new class ofThreadChannel that seems actually pretty similar to poco’s notification queue. you can send any object from a thread and receive it blocking or not blocking from another. there’s some examples in examples/utils