# CriticalSection in the Synthesizer class

**URL:** <https://forum.juce.com/t/criticalsection-in-the-synthesizer-class/53961>\
**Category:** Development\
**Created:** [December 1, 2022, 11:51am UTC](https://forum.juce.com/t/criticalsection-in-the-synthesizer-class/53961 "2022-12-01T11:51:32Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Aapo](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/aapo/32/12195_2.png) [@Aapo](https://forum.juce.com/u/Aapo)\
**Post date:** [December 1, 2022, 11:51am UTC](https://forum.juce.com/t/criticalsection-in-the-synthesizer-class/53961/1 "2022-12-01T11:51:32Z")

</div>

Hi all,

I’ve been working on a sampler built on top of the `juce::Synthesizer` class. In many functions the Synthesizer locks a mutex, including at the start of the processing callback. I was wondering why this is used and how is it real time safe? In the docs it says “This is used to control access to the rendering callback and the note trigger methods.” but who else is calling them? I haven’t come across any audio dropouts or glitches so far but isn’t there a possibility of priority inversion if the audio thread tries to render while another thread has the mutex locked? For example, when adding sounds. I actually need to share a container between the audio and the UI thread and I was wondering if I can use the same mutex to control access to that as well. I might be missing something so I’d really appreciate it if someone could enlighten me about this.

Cheers,  
Aapo

---

<div class="post-metadata">

**Author:** ![TypeWriter](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/typewriter/32/14323_2.png) [@TypeWriter](https://forum.juce.com/u/TypeWriter)\
**Post date:** [December 30, 2022, 12:57am UTC](https://forum.juce.com/t/criticalsection-in-the-synthesizer-class/53961/2 "2022-12-30T00:57:57Z")

</div>

I wouldn’t use the synth mutex on the UI, I assumed it was because it was all in one thread. It was a safety precaution that would effect realtime performance too much.

I have 3 oscillators and 3 wavetables and shared them by reference between UI and Audio. If you have all audio output suspended, and painting suspended {or not being called yet} while creating the memory and writing to it, I think it is safe to read data between threads, as long as it is not currently being written to by another thread, unless it’s atomic.

I use a mutex and scoped lock while loading files to a buffer, so that the same function doesn’t get called while the other one is completing by a user hammering a button several times in a row. Within that mutex function I use an atomic bool that suspends the audio. If you’re using Juce buffers I think you can just call suspend.

If you need realtime audio output, I would just fifo buffers. Then you are never reading and writing from two different places at the same time. ~ torn binary ~

Hope that helps.

---

<div class="post-metadata">

**Author:** ![eyalamir](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/eyalamir/32/2978_2.png) [@eyalamir](https://forum.juce.com/u/eyalamir)\
**Post date:** [December 30, 2022, 1:54am UTC](https://forum.juce.com/t/criticalsection-in-the-synthesizer-class/53961/3 "2022-12-30T01:54:39Z")

</div>

Yes, I agree. I think those locks should probably be replaced by SpinLocks or removed entirely.

Having that said @fr810 recently posted that that might not be an actual problematic issue:

> [@Mutex in the processBlock?](https://forum.juce.com/t/mutex-in-the-processblock/53590/4):
>
> I’d just like to add that all modern OSes use double lock checking for general purpose mutexes (for example JUCE’s CriticalSection), i.e. they first check if a lock is held with a cheap atomic memory operation. Only if it is contended do they need to call an expensive system call. This means that a lock that is never contended is perfectly ok to use in realtime code. Obviously, a lock that is never contended is a bit useless, but we often need to use locks to guard against exceptional behaviour…
