# Method to find if the audio thread is running and calling processBlock?

**URL:** <https://forum.juce.com/t/method-to-find-if-the-audio-thread-is-running-and-calling-processblock/59409>\
**Category:** General JUCE discussion\
**Created:** [January 6, 2024, 2:42pm UTC](https://forum.juce.com/t/method-to-find-if-the-audio-thread-is-running-and-calling-processblock/59409 "2024-01-06T14:42:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Qfactor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/qfactor/32/14921_2.png) [@Qfactor](https://forum.juce.com/u/Qfactor)\
**Post date:** [January 6, 2024, 2:42pm UTC](https://forum.juce.com/t/method-to-find-if-the-audio-thread-is-running-and-calling-processblock/59409/1 "2024-01-06T14:42:24Z")

</div>

I seem to remember there was a method in JUCE that returns if AudioProcessor::processBlock is being called. Am I remembering well? Alternatively, is there a safe way to know that from a separate thread even in the event of some crazy hosts skipping prepareToPlay()?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [January 6, 2024, 2:46pm UTC](https://forum.juce.com/t/method-to-find-if-the-audio-thread-is-running-and-calling-processblock/59409/2 "2024-01-06T14:46:12Z")

</div>

> [@Qfactor](#):
>
> considering that crazy hosts may skip prepareToPlay()?

That would break the contract, so this host would be considered broken.  
It is not specified, that each time before resuming playback it would be called. But it has to be called at least once before playing.

processBlock can be called in realtime or as fast as possible when bouncing a track.  
You can save the wall clock in processBlock into an atomic, so you can guess from that.  
But take care that you choose a wall clock (aka system time) that is realtime safe. Some of them are blocking and might do a priorty inversion.

---

<div class="post-metadata">

**Author:** ![Qfactor](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/qfactor/32/14921_2.png) [@Qfactor](https://forum.juce.com/u/Qfactor)\
**Post date:** [January 6, 2024, 3:18pm UTC](https://forum.juce.com/t/method-to-find-if-the-audio-thread-is-running-and-calling-processblock/59409/4 "2024-01-06T15:18:05Z")

</div>

I’ve just realized an atomic in the processBlock is indeed enough to indicate when it starts running 🙂

I still have to find a way though to know when it stops running.

Can I assume releaseResources() will be called if the plugin stops calling processBlock (e.g. if it is suspended or bypassed but the user still has access to its UI)?

---

<div class="post-metadata">

**Author:** ![daniel](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.juce.com/daniel/32/790_2.png) [@daniel](https://forum.juce.com/u/daniel)\
**Post date:** [January 6, 2024, 3:27pm UTC](https://forum.juce.com/t/method-to-find-if-the-audio-thread-is-running-and-calling-processblock/59409/5 "2024-01-06T15:27:30Z")

</div>

> [@Qfactor](#):
>
> Can I assume releaseResources() will be called if the plugin stops calling processBlock (e.g. if it is suspended or bypassed but the user still has access to its UI)?

Unfortunately no. There are no guarantees, some hosts will only call releaseResources() immediately before destroying your plugin.

---

<div class="post-metadata">

**Author:** ![JussiNeuralDSP](https://avatars.discourse-cdn.com/v4/letter/j/c68b51/32.png) [@JussiNeuralDSP](https://forum.juce.com/u/JussiNeuralDSP)\
**Post date:** [July 2, 2024, 1:43pm UTC](https://forum.juce.com/t/method-to-find-if-the-audio-thread-is-running-and-calling-processblock/59409/6 "2024-07-02T13:43:45Z")

</div>

Here’s a little helper class:

```auto
thread_local static bool processDetectorProcessing;
thread_local static bool processDetectorRealTime;

class ProcessDetector
{
public:
    ProcessDetector(juce::AudioProcessor* processor)
    {
        processDetectorRealTime = !processor->isNonRealtime();
        processDetectorProcessing = true;
    }

    ~ProcessDetector()
    {
        processDetectorRealTime = false;
        processDetectorProcessing = false;
    }

    //! Return true if we are currently inside processBlock
    static bool isProcessing()
    {
        return processDetectorProcessing;   
    }

    //! Return true if we are currently inside processBlock in real-time mode
    static bool isProcessingRealTime()
    {
        return processDetectorRealTime;   
    }
};

```

Use it like this:

```auto
void MyProcessor::processBlock(juce::AudioBuffer<float>& audio, juce::MidiBuffer& midi)
{
    ProcessDetector processDetector(this);

    // process your audio here
}

```

In your code you can check the processing status:

```auto
if (ProcessDetector::isProcessingRealTime())
{
    // we are inside processBlock in real-time mode
}

```
