Thread Safety
x37v opened this issue · 4 comments
Maybe I just can't find it, but is there any information published about thread safety in libossia
?
I'm curious about both the "safe" and "fast" c++ implementations.
Should I only update parameter values from a single thread or can I do it from multiple threads (do I need to wrap in a lock)?
The C API has some documentation about it: http://ossia.io/libossia/html/group___c_a_p_i.html
Which pretty much translates to the rest.
In short:
- sending / receiving values is thread-safe, e.g. you can call push_value / pull_value from any thread, it's locked inside
- anything else must be protected: you can't create / change the attributes of a "tree of nodes" across multiple threads basically (this covers 99% of the use cases where the app creates the nodes on startup or from the ui thread basically).
- callbacks, for instance when getting a value, may happen from any random thread, likely some network I/O thread (and in my experience this has been the main cause of threading issues one could have - don't go for instance updating a UI widget in Qt or GTK directly from one of those callbacks as they won't support it).
A useful class is http://ossia.io/libossia/html/message__queue_8hpp_source.html which allows to go from the "callbacks fired from threads" to the "getting all the messages in a queue in the main thread" model, which is often simpler and has a reasonable performance impact if you aren't sending millions of messages per second.
Okay, this C API doc is very helpful, thanks @jcelerier !
could we close this issue @jcelerier ?
I think so - sorry for lagging a bit behind on libossia, trying to get back to it by the end of the week