libremidi icon indicating copy to clipboard operation
libremidi copied to clipboard

Thread-Safety

Open Holzhaus opened this issue 4 years ago • 2 comments

Is it safe to write data to a device from multiple threads at the same time?

Holzhaus avatar Mar 20 '21 20:03 Holzhaus

It isn't - and from a cursory glance the underlying APIs are mostly not thread-safe either.

I think that the best course of action is to serialize things in your software by using its event loop mechanism if any - by using https://github.com/max0x7ba/atomic_queue or https://github.com/cameron314/concurrentqueue it should be doable without any locks and with pretty good performance

jcelerier avatar Mar 21 '21 10:03 jcelerier

here's a very simple and largely improvable example of how to do it, for reference: https://github.com/jcelerier/libremidi/blob/master/tests/multithread_midiout.cpp

jcelerier avatar Mar 22 '21 11:03 jcelerier

see https://github.com/jcelerier/libremidi/blob/87a16babd048ba20c0bbff9e8c8d48d7afc965ab/examples%2Fmultithread_midiout.cpp - I'm tempted to close and leave as this as I really think this synchronization is better done outside of the library rather than inside

jcelerier avatar Feb 07 '24 19:02 jcelerier