Gin
Loading...
Searching...
No Matches
CoalescedTimer Class Reference

An efficient timer that shares a single juce::Timer between multiple instances. More...

#include <gin_coalescedtimer.h>

Public Member Functions

 CoalescedTimer ()=default
 Creates a stopped CoalescedTimer.
 ~CoalescedTimer ()
 Destructor.
void startTimer (int ms)
 Starts the timer with the specified interval in milliseconds.
void startTimerHz (int hz)
 Starts the timer with the specified frequency in Hz.
void stopTimer ()
 Stops the timer.

Public Attributes

std::function< void()> onTimer
 Callback function called at the specified interval.

Detailed Description

An efficient timer that shares a single juce::Timer between multiple instances.

Instead of each timer instance creating its own juce::Timer thread, this class coalesces all timers with the same interval into a single shared juce::Timer. This reduces overhead when you have many timers running at the same rate.

Benefits:

  • Lower CPU usage when running many timers
  • Reduced thread creation overhead
  • Automatic cleanup when not in use

Example usage:

timer.onTimer = [this] { updateUI(); };
timer.startTimerHz (60); // 60 Hz refresh rate
void startTimerHz(int hz)
Starts the timer with the specified frequency in Hz.
Definition gin_coalescedtimer.h:69
CoalescedTimer()=default
Creates a stopped CoalescedTimer.
std::function< void()> onTimer
Callback function called at the specified interval.
Definition gin_coalescedtimer.h:92
See also
startTimer, startTimerHz, stopTimer

Constructor & Destructor Documentation

◆ CoalescedTimer()

CoalescedTimer::CoalescedTimer ( )
default

Creates a stopped CoalescedTimer.

◆ ~CoalescedTimer()

CoalescedTimer::~CoalescedTimer ( )

Destructor.

Stops the timer if running.

References stopTimer().

Member Function Documentation

◆ startTimer()

void CoalescedTimer::startTimer ( int ms)

Starts the timer with the specified interval in milliseconds.

If the timer is already running, it will be restarted with the new interval. Multiple CoalescedTimer instances with the same interval will share a single underlying juce::Timer.

Parameters
msThe timer interval in milliseconds

References stopTimer().

Referenced by startTimerHz().

◆ startTimerHz()

void CoalescedTimer::startTimerHz ( int hz)

Starts the timer with the specified frequency in Hz.

This is a convenience method that converts frequency to interval. For example, 60 Hz becomes approximately 16.67 milliseconds.

Parameters
hzThe desired frequency in Hz (must be > 0, or timer will stop)

References startTimer(), and stopTimer().

◆ stopTimer()

void CoalescedTimer::stopTimer ( )

Stops the timer.

The onTimer callback will no longer be called. If this was the last CoalescedTimer using this interval, the shared juce::Timer is deleted.

Referenced by startTimer(), startTimerHz(), and ~CoalescedTimer().

Member Data Documentation

◆ onTimer

std::function<void ()> CoalescedTimer::onTimer

Callback function called at the specified interval.

Set this to a lambda or function to be executed when the timer fires.


The documentation for this class was generated from the following file: