# MidiMessage & lock free?

**URL:** <https://forum.juce.com/t/midimessage-lock-free/30484>\
**Category:** General JUCE discussion\
**Created:** [November 22, 2018, 12:39pm UTC](https://forum.juce.com/t/midimessage-lock-free/30484 "2018-11-22T12:39:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![alatar](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@alatar](https://forum.juce.com/u/alatar)\
**Post date:** [November 22, 2018, 12:39pm UTC](https://forum.juce.com/t/midimessage-lock-free/30484/1 "2018-11-22T12:39:23Z")

</div>

I am just looking into the MidiMessage class, because I want to program an arpeggiator with JUCE.

I realized, that MidiMessage is using malloc internally. As far as I know, malloc is not lock free.  
So you are not allowed to create or copy a MidiMessage on the audio thread.  
Am I correct here?

How do you guys deal with that? Do you create a separate Midi thread, which is separated from the audio thread? What do you think is the best way?

---

<div class="post-metadata">

**Author:** ![martingasser](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@martingasser](https://forum.juce.com/u/martingasser)\
**Post date:** [November 22, 2018, 1:32pm UTC](https://forum.juce.com/t/midimessage-lock-free/30484/2 "2018-11-22T13:32:47Z")

</div>

MidiMessage preallocates 8 bytes on a 64 bit system, see below, from `juce_MidiMessage.h`. The maximum length of standard MIDI messages is 3 bytes.

```
union PackedData
{
    uint8* allocatedData;
    uint8 asBytes[sizeof (uint8*)];
};

```

It only allocates dynamically for larger messages (Sysex). Of course, you shouldn’t do that on the audio thread.

---

<div class="post-metadata">

**Author:** ![alatar](https://avatars.discourse-cdn.com/v4/letter/a/34f0e0/32.png) [@alatar](https://forum.juce.com/u/alatar)\
**Post date:** [November 22, 2018, 2:02pm UTC](https://forum.juce.com/t/midimessage-lock-free/30484/3 "2018-11-22T14:02:27Z")

</div>

Ah, I see.  
Did not realize that.  
So normal messages are OK, Sysex is not OK?  
Thanks for the clarification.

---

<div class="post-metadata">

**Author:** ![tomto66](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/tomto66/32/4916_2.png) [@tomto66](https://forum.juce.com/u/tomto66)\
**Post date:** [November 22, 2018, 2:06pm UTC](https://forum.juce.com/t/midimessage-lock-free/30484/4 "2018-11-22T14:06:08Z")

</div>

I would be very surprised this is correct. As far as I understand all incoming midi messages are fwd into the processor hence in the audio thread. Those may include sysex. How and where (on which thread) are those created then? (Then again, by all means surprise me!)

---

<div class="post-metadata">

**Author:** ![martingasser](https://avatars.discourse-cdn.com/v4/letter/m/e274bd/32.png) [@martingasser](https://forum.juce.com/u/martingasser)\
**Post date:** [November 22, 2018, 2:34pm UTC](https://forum.juce.com/t/midimessage-lock-free/30484/5 "2018-11-22T14:34:05Z")

</div>

I didn’t say that it doesn’t happen in practice, I only said that dynamic allocation on the audio thread is to be avoided.

For instance, if you look at `MidiBuffer::Iterator::getNextEvent()`, you can see that it _will_ allocate for large data chunks.

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [November 22, 2018, 2:36pm UTC](https://forum.juce.com/t/midimessage-lock-free/30484/6 "2018-11-22T14:36:54Z")

</div>

In the question it was asked to create an arpeggiator, which means creating simple midi messages inside the processBlock. And I would agree this is safe to do with simple noteOn, or controller messages.

However you are correct, as far as I can see in the code, if the midi device were to send a chunk of sysex data, this would in the JUCE wrapper trigger a MidiMessage::createSysex(), which involves a HeapBlock.

> <https://github.com/WeAreROLI/JUCE/blob/master/modules/juce_audio_plugin_client/VST3/juce_VST3_Wrapper.cpp#L2249>

leads to:

> <https://github.com/WeAreROLI/JUCE/blob/d51a44a58ec686b627d38168e301840288674645/modules/juce_audio_processors/format_types/juce_VST3Common.h#L484>

which is:

> <https://github.com/WeAreROLI/JUCE/blob/master/modules/juce_audio_basics/midi/juce_MidiMessage.cpp#L659>

This is a use case that might be worth looking into. Maybe there could be a flag to skip SysEx, if it is sent to an AudioProcessor for realtime consumption, but I am not expert enough in midi and all the use cases to make a call on that.

---

<div class="post-metadata">

**Author:** ![leehu](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/leehu/32/3280_2.png) [@leehu](https://forum.juce.com/u/leehu)\
**Post date:** [November 23, 2018, 10:34am UTC](https://forum.juce.com/t/midimessage-lock-free/30484/7 "2018-11-23T10:34:05Z")

</div>

this is an interesting point and very useful to know!
