Is there any delay function in JUCE?

Hi there,
Is there any delay function in JUCE? After one statement has been excecuted, call the delay function to delay some time and then excecute the next staement.

You can use the functions of the normal thread libraries for that, the thread abstraction is Thread, and Thread::sleep() exists.

However in current multi processor setups, this is bad practice, since you want other threads give the chance to use this time instead of halting the execution.

The best way is to use Timer for recurring tasks, or the Timer::callAfterDelay() to do something, well after a delay…

For background jobs, best to use TimeSliceThreads, which can define, when they want to be called again by the return value.

Hope that helps

1 Like

Using the OS sleep functions doesn’t consume CPU and steal it from other threads. Wouldn’t make much sense to call the function “sleep” otherwise…

However, using the sleep function is almost always a mistake these days for other reasons. It doesn’t make sense in the GUI thread as that would just block the GUI event loop from running. For obvious reasons, it never should be used in the audio thread. There might be some use cases within separate worker threads, but even in those something else would be preferable, like using condition variables, or using the OS thread yield function instead of sleep.

1 Like

That’s what I meant to bring across, imagining someone calls sleep on the message thread on a regular basis… :slight_smile:

1 Like

juce::BitCoinMiner::processForSeconds(10.0);

7 Likes