# MidiBuffer + Critical Section

**URL:** <https://forum.juce.com/t/midibuffer-critical-section/7305>\
**Category:** General JUCE discussion\
**Created:** [August 15, 2011, 9:59am UTC](https://forum.juce.com/t/midibuffer-critical-section/7305 "2011-08-15T09:59:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![solar3d](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/solar3d/32/135_2.png) [@solar3d](https://forum.juce.com/u/solar3d)\
**Post date:** [August 15, 2011, 9:59am UTC](https://forum.juce.com/t/midibuffer-critical-section/7305/1 "2011-08-15T09:59:13Z")

</div>

Hi,

my plugin generates Midi messages to output to a physical Midi output.  
To do so, I use a public MidiBuffer to what I add all the messages to output and let a  
background thread then swaps this public MidiBuffer with the background  
thread its private MidiBuffer. Then the thread iterates that private buffer to sends its messages  
to the physical output and clears the public MidiBuffer afterwards.

Since I also have to THRU incoming Midi events, another thread (Midi input callback) is involved  
and will probably add some incoming events to the public MidiBuffer while  
this buffer is being iterated or cleared or swapped. Aslo the user will probably add Midimessages  
to the buffer at the same time when he drag a slider of the UI.  
So I have to make the whole thing thread safe….

Can’t use MidiMessageCollector here (which has thread safe methods) because Non Realtime Messages  
i.e. Sysex can go to the MidiBuffer as well.

My first idea was to create my own buffer class inherited from MidiBuffer and Critical Section  
because I read in another C++ forum that this is a possible way.

What does the Juce experts say about this? Is it a common way or rather a stupid thing to do  
and if so, would you please point me to the right way?

Thank you!  
Joerg

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 15, 2011, 11:23am UTC](https://forum.juce.com/t/midibuffer-critical-section/7305/2 "2011-08-15T11:23:12Z")

</div>

For god’s sake don’t go inheriting from MidiBuffer or CriticalSection!

[http://www.gotw.ca/publications/mill06.htm](http://www.gotw.ca/publications/mill06.htm)

You can of course _use_ a midibuffer and a criticalsection together to do that job if you want to, and it’ll probably work fine. Might be smarter to use a circular buffer of MidiMessages though, using the AbstractFifo class.

---

<div class="post-metadata">

**Author:** ![solar3d](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/solar3d/32/135_2.png) [@solar3d](https://forum.juce.com/u/solar3d)\
**Post date:** [August 15, 2011, 4:23pm UTC](https://forum.juce.com/t/midibuffer-critical-section/7305/3 "2011-08-15T16:23:19Z")

</div>

Thank you for your suggestion and that nice link!  
As a not native English speaking German it isn’t always easy to understand  
programmer guides but this one is really clear and understandable.

Joerg

---

<div class="post-metadata">

**Author:** ![solar3d](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/solar3d/32/135_2.png) [@solar3d](https://forum.juce.com/u/solar3d)\
**Post date:** [August 19, 2011, 9:20am UTC](https://forum.juce.com/t/midibuffer-critical-section/7305/4 "2011-08-19T09:20:49Z")

</div>

Hi,

I think ScopedLock or TryScopedLock will do the trick here.

Just one question:  
Since I have to cope with input and output buffers here and don’t want to lock  
the input buffer while adding events to the output buffer and vice versa,  
I would like to use 2 x Critical Section in on class, one for input and one for output.

Would that be okay?

Thanks Joerg
