# MidiMessageSequence list thread safety

**URL:** <https://forum.juce.com/t/midimessagesequence-list-thread-safety/26523>\
**Category:** General JUCE discussion\
**Created:** [February 22, 2018, 8:41pm UTC](https://forum.juce.com/t/midimessagesequence-list-thread-safety/26523 "2018-02-22T20:41:25Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [February 22, 2018, 8:41pm UTC](https://forum.juce.com/t/midimessagesequence-list-thread-safety/26523/1 "2018-02-22T20:41:25Z")

</div>

Shouldn’t the list OwnedArray use a CriticalSection – or should you template the MidiMessageSequence class to allow us to set if the OwnedArray is thread safe?

Thanks,

Rail

---

<div class="post-metadata">

**Author:** ![jonathonracz](https://avatars.discourse-cdn.com/v4/letter/j/da6949/32.png) [@jonathonracz](https://forum.juce.com/u/jonathonracz)\
**Post date:** [February 22, 2018, 9:11pm UTC](https://forum.juce.com/t/midimessagesequence-list-thread-safety/26523/2 "2018-02-22T21:11:37Z")

</div>

I assume you’re going between the audio and message threads? Avoid locks! That’s why the `swapWith` method exists - lock for an extremely short and predictable amount of time to quickly swap two copies of the `MidiMessageSequence` any time the model changes in the message thread. I just coded something like this and it works great. Several audio-centric JUCE structures which allow modify options under the hood (notably `AudioProcessorGraph`) also use this method.

Unless you’re doing something new and crazy?

---

<div class="post-metadata">

**Author:** ![railjonrogut](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/railjonrogut/32/505_2.png) [@railjonrogut](https://forum.juce.com/u/railjonrogut)\
**Post date:** [February 22, 2018, 9:27pm UTC](https://forum.juce.com/t/midimessagesequence-list-thread-safety/26523/3 "2018-02-22T21:27:38Z")

</div>

I actually do that – but have run into an issue about 1% of the time when doing this under Windows (even less under MacOS).

I should point out that the issue I have isn’t between the audio thread and the message thread - I have a worker thread which updates the sequence.

Rail
