# Which thread calls audioDeviceAboutToStart, ...Stopped?

**URL:** https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594
**Category:** General JUCE discussion
**Created:** [March 19, 2011, 4:39pm UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594 "2011-03-19T16:39:19Z")
**Posts on this page:** 8
**Page:** 1

<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: [March 19, 2011, 4:39pm UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594/1 "2011-03-19T16:39:19Z")

</div>

`AudioIODeviceCallback::audioDeviceAboutToStart (AudioIODevice* device)
AudioIODeviceCallback::audioDeviceStopped ()`

Does Juce specify which threads these will be called on? And if not, can we get some assurances?

Client code that wants to use the “device” parameter from audioDeviceAboutToStart may be simplified if Juce can guarantee that audioDeviceAboutToStart only gets called from the thread which is calling into the AudioDeviceManager (the documentation is silent on this topic).

---

<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: [March 20, 2011, 10:14am UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594/2 "2011-03-20T10:14:22Z")

</div>

No, I don’t think I can give any guarantees about that - there are too many possible platforms and device peculiarities to deal with. None of the calls to any of the AudioIODeviceCallback methods will overlap each other, but they could happen on any thread.

---

<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: [March 20, 2011, 5:06pm UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594/3 "2011-03-20T17:06:56Z")

</div>

The issue is that when I create new audio objects after a device has been started, I need to call prepareToPlay() with appropriate parameters for buffer size and sample rate. These come from the AudioIODevice pointer. Ideally, I would like to prepare these new audio objects outside of the callback so the burden of construction and initialization does not fall on the hardware thread.

Can I assume that the only way for an AudioIODevice pointers to become invalid is through invoking a member of AudioDeviceManager? If that is the case then I can protect the entire AudioDeviceManager with a critical section.

---

<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: [March 20, 2011, 5:09pm UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594/4 "2011-03-20T17:09:29Z")

</div>

> [@](#):
>
> Can I assume that the only way for an AudioIODevice pointers to become invalid is through invoking a member of AudioDeviceManager?

hmm… probably not 100% safe - e.g. a device could stop working if it gets unplugged or something.

---

<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: [March 20, 2011, 6:30pm UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594/5 "2011-03-20T18:30:04Z")

</div>

> [@jules](#):
>
> [quote]Can I assume that the only way for an AudioIODevice pointers to become invalid is through invoking a member of AudioDeviceManager?

hmm… probably not 100% safe - e.g. a device could stop working if it gets unplugged or something.[/quote]

How do you suggest I call prepareToPlay with the right parameters, from outside the audio 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: [March 20, 2011, 7:39pm UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594/6 "2011-03-20T19:39:52Z")

</div>

I’d suggest doing it in audioDeviceAboutToStart - the whole point of the method is that it’s where you can set-up your stuff. I can’t guarantee the thread that will call it, but I would say that it’s ok to spend some time doing work in there.

---

<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: [March 20, 2011, 8:12pm UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594/7 "2011-03-20T20:12:39Z")

</div>

> [@jules](#):
>
> I’d suggest doing it in audioDeviceAboutToStart - the whole point of the method is that it’s where you can set-up your stuff. I can’t guarantee the thread that will call it, but I would say that it’s ok to spend some time doing work in there.

Yes of course, but I’m talking about audio components that are created AFTER the initial call to audioDeviceAboutToStart. For example, the user adds a new PositionableAudioSource into their AudioProcessorGraph.

Just an example. I’m not using the audio graph stuff. In my case I have to manually call prepareToPlay() with the “current” buffer size and sample rate. These values come from the device pointer.

---

<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: [March 20, 2011, 8:39pm UTC](https://forum.juce.com/t/which-thread-calls-audiodeviceabouttostart-stopped/6594/8 "2011-03-20T20:39:05Z")

</div>

Well, personally I’d probably use a CallbackMessage or something to do it on the message thread.
