Sign up for the KDAB Newsletter
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Another day, another blog post about KDToolBox, KDAB's collection of miscellaneous useful C++ classes and stuff.
Today, we'll talk about ways to throttle your signal/slots connections -- in other words, how to activate a slot less often than the emission rate of the signal it's connected to. The usual reason why you may want something like this is performance. Invoking a slot at a high frequency may be too expensive for your application, so you need to rate-limit the slot's activation.
That sounds very simple to achieve. All you need is love a timer and a little logic to (re)start the timer when a signal is received, then actually trigger the slot when the timer fires. However, as for many things, the devil is in the details.
In the industry, these rate-limiting behaviors have well-established names and behaviors: throttlers and debouncers (see here for some excellent examples).
Furthermore, throttlers and debouncers can be trailing or leading.
You'll be glad to know that we have pushed an implementation of throttlers and debouncers into KDToolBox.
Their usage is very simple. First and foremost, create an object of the right kind and set it up:
// trailing throttler, rate limiting at 1 emission every 100ms
KDSignalThrottler *throttler = new KDSignalThrottler(parent);
throttler->setTimeout(100ms);
Then, place the throttler as an "intermediary" object between the signal that should be rate-limited and the slot that needs to be activated. You need to connect your signal to the throttle()
slot of the throttler and the throttler's own triggered()
signal to the actual slot:
connect(sender, &Sender::signal, throttler, &KDSignalThrottler::throttle);
connect(throttler, &KDSignalThrottler::triggered, receiver, &Receiver::slot);
And voilà! Now, your slot activations will be rate-limited at a frequency of 10Hz, or less.
KDToolBox is freely available here. Thanks for reading, and have a great day!
About KDAB
The KDAB Group is a globally recognized provider for software consulting, development and training, specializing in embedded devices and complex cross-platform desktop applications. In addition to being leading experts in Qt, C++ and 3D technologies for over two decades, KDAB provides deep expertise across the stack, including Linux, Rust and modern UI frameworks. With 100+ employees from 20 countries and offices in Sweden, Germany, USA, France and UK, we serve clients around the world.
Stay on top of the latest news, publications, events and more.
Go to Sign-up
Learn Modern C++
Our hands-on Modern C++ training courses are designed to quickly familiarize newcomers with the language. They also update professional C++ developers on the latest changes in the language and standard library introduced in recent C++ editions.
Learn more
2 Comments
7 - Apr - 2021
Julien
That's indeed useful from time to time !
What about making it even easier to use, with a single function call, similar to the static QObject::connect :
This could automatically instantiate a KDSignalThrotller, connect the signal and slot to it, and make sure the throtller is deleted as soon as either connection is broken.
As a user, this would make throttled connection as easy and safe to use as a regular connection.
12 - Apr - 2021
Giuseppe D'Angelo
Hi,
That sounds like a nice idea. Mind filing it as a suggestion on GitHub? :) Thanks.