# Loading AudioProcessorGraph state from a background thread

**URL:** <https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451>\
**Category:** General JUCE discussion\
**Created:** [January 21, 2016, 11:19am UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451 "2016-01-21T11:19:54Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![masshacker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/masshacker/32/650_2.png) [@masshacker](https://forum.juce.com/u/masshacker)\
**Post date:** [January 21, 2016, 11:19am UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/1 "2016-01-21T11:19:54Z")

</div>

Hi,

I’m facing issues when trying to load nodes and connections into an AudioProcessorGraph from a background thread.

Basically what I want to do is this:

- Instantiate an AudioProcessorGraph on the main thread

- Start a Thread in the background

- Add a bunch of nodes within the Thread::run() callback, and update a progress bar on the main thread

- After all the nodes are added, connect them by adding a bunch of connections to the graph in the background thread, while updating the progress bar on the main thread

&nbsp;

What happens is that every time I add a node or a connection to the graph, the graph triggers an async update to rebuild its ops, which makes impossible to determine when all nodes or connections are done being added.

As a result, I get a crash (bad access) in ConnectionSorter::compareElements when I add the connections from the background thread.

&nbsp;

What I’d like to have is a way to make the graph stop calling triggerAsyncUpdate() temporarily, add my nodes and connections, and after that call triggerAsyncUpdate() just once.

Unfortunately I don’t see anything I could do without&nbsp;modifications to the AudioProcessorGraph class.

Any thoughts?

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [January 26, 2016, 10:22am UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/2 "2016-01-26T10:22:30Z")

</div>

JUCE currently does not support the multi-threaded use of the AudioProcessorGraph. However, some other forum users have implented their own processor graphs which seem to work quite well. See [this thread](http://www.juce.com/forum/topic/multithreaded-audioprocessorgraph-source-code) for more info.

---

<div class="post-metadata">

**Author:** ![chkn](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/chkn/32/3862_2.png) [@chkn](https://forum.juce.com/u/chkn)\
**Post date:** [January 26, 2016, 11:57am UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/3 "2016-01-26T11:57:00Z")

</div>

I think this thread is more about problems using&nbsp;the current (single-core) AudioProcessorGraph in an a threading-context. My (proof-of-concept)&nbsp;AudioProcessorGraph is more about balancing the CPU-load of the nodes over&nbsp;multiple cpu-cores.

&nbsp;

---

<div class="post-metadata">

**Author:** ![olilarkin](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/olilarkin/32/7142_2.png) [@olilarkin](https://forum.juce.com/u/olilarkin)\
**Post date:** [July 17, 2016, 11:28am UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/4 "2016-07-17T11:28:04Z")

</div>

+1 it would be great to be able to disable AsyncUpdates on the graph

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [July 19, 2016, 9:26am UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/5 "2016-07-19T09:26:11Z")

</div>

This sounds like a good idea and I can definitely add that. However, doesn’t all the initialisation happen inside the `buildRenderingSequence` callback anyway. This is where all the `prepareToPlay` callbacks will happen and this is where most plug-ins load their resources, right?

---

<div class="post-metadata">

**Author:** ![masshacker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/masshacker/32/650_2.png) [@masshacker](https://forum.juce.com/u/masshacker)\
**Post date:** [July 20, 2016, 7:59am UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/6 "2016-07-20T07:59:28Z")

</div>

Yes, you are right, plug-ins get definitely initialised there.  
But in this way they would be initialised just once, after all nodes and connections are added to the graph.  
It would be really great if you could add a way to disable the async updates temporarily.

---

<div class="post-metadata">

**Author:** ![fr810](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/fr810/32/841_2.png) [@fr810](https://forum.juce.com/u/fr810)\
**Post date:** [July 20, 2016, 5:21pm UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/7 "2016-07-20T17:21:15Z")

</div>

OK I’ve added a change on develop so that the graph will not be rebuilt if the AudioProcessingGraph is not prepared, i.e. if prepareToPlay hasn’t been called yet or releaseResources was called. You can then add all your plug-ins after a releaseResources call (or right after constructing the graph). Once the plug-ins have been added, you just need to call prepareToPlay. Does this work for you?

---

<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:** [July 20, 2016, 5:24pm UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/8 "2016-07-20T17:24:24Z")

</div>

Won’t this break my suggested AudioProcessorGraph PDC changes?

> [@AudioProcessorGraph PDC and changing latency](https://forum.juce.com/t/audioprocessorgraph-pdc-and-changing-latency/18121/3):
>
> BTW this doesn’t set the total latency correctly for reporting to the Host (which will be my next task)… It does however correctly delay all the processors in the AudioProcessorGraph. Rail

Rail

---

<div class="post-metadata">

**Author:** ![masshacker](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/masshacker/32/650_2.png) [@masshacker](https://forum.juce.com/u/masshacker)\
**Post date:** [July 21, 2016, 3:24pm UTC](https://forum.juce.com/t/loading-audioprocessorgraph-state-from-a-background-thread/16451/9 "2016-07-21T15:24:09Z")

</div>

Yes, thanks, this works for me.  
I don’t know if this can affect Rail’s changes though.
