# ValueTree locking

**URL:** <https://forum.juce.com/t/valuetree-locking/6707>\
**Category:** General JUCE discussion\
**Created:** [April 13, 2011, 12:51pm UTC](https://forum.juce.com/t/valuetree-locking/6707 "2011-04-13T12:51:29Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [April 13, 2011, 12:51pm UTC](https://forum.juce.com/t/valuetree-locking/6707/1 "2011-04-13T12:51:29Z")

</div>

I was wondering about locking a ValueTree in some good way. I have a situation where one value tree can be modified by 3 threads (it’s an audio plugin), so it’s the MAIN gui thread, the audio thread and the MIDI INPUT thread (from a midi device). I try to move everything to the value tree and it’s properties. I was wondering if it will be enough to lock the setProperty and getProperty methods, and how do i deal with the listener methods (valuepropertychanged), are those methods called in a async or sync way, should i add a lock to them too (this is where i react to changes so a lot of work is done in that callback).

---

<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:** [April 13, 2011, 12:59pm UTC](https://forum.juce.com/t/valuetree-locking/6707/2 "2011-04-13T12:59:36Z")

</div>

Ooh, they’re not really designed for that kind of multi-threadedness. The tricky bit is that the callbacks are all synchronous, so changing a value in your audio thread could end up calling all kinds of other code. Nasty!

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [April 13, 2011, 2:16pm UTC](https://forum.juce.com/t/valuetree-locking/6707/3 "2011-04-13T14:16:54Z")

</div>

so what’s the best way to do this. Maybe i’ll explain how i designed this. I was thinking of adding a readLock for getProperty and writeLock for setProperty, the locks work on the modulatorCriticalSection.

[attachment=0]modulator.jpg[/attachment]

---

<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:** [April 13, 2011, 3:12pm UTC](https://forum.juce.com/t/valuetree-locking/6707/4 "2011-04-13T15:12:21Z")

</div>

Using any kind of lock in your audio thread is probably a bad idea. Personally, I’d suggest making your ValueTree accessible only by the UI thread, and using some other mechanism to communicate with the other threads.

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [April 13, 2011, 8:38pm UTC](https://forum.juce.com/t/valuetree-locking/6707/5 "2011-04-13T20:38:24Z")

</div>

How about:

- when a thread wants to change the value of the modulator, it sets an Atomic value and triggers an async update
- when the handleAsyncUpdate happens, the modulator picks up the change and acts accordingly

would that work ?

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [April 13, 2011, 8:57pm UTC](https://forum.juce.com/t/valuetree-locking/6707/6 "2011-04-13T20:57:02Z")

</div>

Post changes to values using a thread-safe queue and process the queue from the thread that owns the ValueTree:

[Thread Queue](http://code.google.com/p/dspfilterscpp/source/browse/trunk/demo/ThreadQueue.h)

When a value changes, in your Listener callback, notify each interested thread by posting a call to it’s thread queue with a copy of the value.

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [April 13, 2011, 9:12pm UTC](https://forum.juce.com/t/valuetree-locking/6707/7 "2011-04-13T21:12:46Z")

</div>

I had a incomming midi queue that was an Array that was CriticalSection locked, but jules just said to not do any locking.

One way or the other any change from any thread will cause an update on the audio thread, and any change from the audio thread will cause locking when using your ThreadQueue.  
It makes me wonder, how much locking in the audio thread is too much, is there any way to deal without locking at all.

---

<div class="post-metadata">

**Author:** ![TheVinn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/thevinn/32/806_2.png) [@TheVinn](https://forum.juce.com/u/TheVinn)\
**Post date:** [April 13, 2011, 9:24pm UTC](https://forum.juce.com/t/valuetree-locking/6707/8 "2011-04-13T21:24:03Z")

</div>

> [@atom](#):
>
> any change from the audio thread will cause locking when using your ThreadQueue.

Right, but the difference is that the lock is held for constant time regardless of the size of the queue:

```auto
  // get the list
  lock ();
  head = m_head;
  m_head = 0;
  m_tail = 0;
  if (head)
    reset();
  unlock ();
```

The lock is held for a very short time.

> [@](#):
>
> It makes me wonder, how much locking in the audio thread is too much, is there any way to deal without locking at all

It is possible to get rid of a lot of locking, especially if you have access to lock-free data structures. But a small amount of locking is okay. Also remember that operator new/delete, malloc(), and free() implementations usually take a global critical section.

Some locking is okay, but when you hold a lock in time proportional to the amount of data being processed that’s when you can start to run into trouble. This goes for any thread, if you lock for a tiny bit in the audio thread but then take the same lock for a long time elsewhere, you will block the audio thread for a long time.

Note that in the thread queue implementation, the critical section is always held for O(1).

---

<div class="post-metadata">

**Author:** ![atom](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/atom/32/113_2.png) [@atom](https://forum.juce.com/u/atom)\
**Post date:** [April 13, 2011, 9:30pm UTC](https://forum.juce.com/t/valuetree-locking/6707/9 "2011-04-13T21:30:53Z")

</div>

well i definetly tried to use no new malloc free delete, that’s been stressed alot on the JUCE forums.
