Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::signals2::signal - is a "decimated" version available?
From: Igor R (boost.lists_at_[hidden])
Date: 2012-05-22 02:12:29


> The signal is sometimes is generated at 100Hz.  Other times it is
> generated at 1Hz.  The dialog provides a "slot" for this signal that
> updates the controls on the screen.  I'd like to limit the "slot rate"
> to 10Hz.  By "slot rate" I mean the rate at which the slot receives
> notification from the signal.  This will prevent the screen updates
> from happening faster than can be seen.
>
> Has this been done already?  If not, I'm going to implement something
> like the pseudocode below.  Currently I'm not sure how I to specify
> "arguments" nor how I am going to specify the function "SlotCallback".
<...>
> Pseudocode:
>
> float MaxPeriod = 0.001; // 10Hz
>
> class SignalDecimator
> {
>
> public:
>
>  void Register(signal Signal, slot Slot)
>  {
>    Signal.connect(&SignalDecimator::SlotCallback, this, _1, _2, _3, _4...);
>
>    mSlot = Slot;
>  }
>
> private:
>
>  void SlotCallback(arguments Arguments)
>  {
>    if (TimeSinceLastCall() > MaxPeriod)
>    {
>      mSlot(Arguments);
>    }
>    else
>    {
>      mArguments = Arguments;
>
>      if (!TimerIsRunning())
>      {
>        StartTimer(MaxPeriod - TimeSinceLastCall());
>      }
>    }
>  }

Do you really need to store arguments and re-signal with timer? If all
you need is just to ignore too frequent signals, you can do this in a
much simpler way:

void SlotCallback(arguments Arguments)
{
  auto now = boost::posix_time::microsec_clock::universal_time();
 Â if (now - lastNotified > boost::posix_time::millisec(100))
     mSlot(Arguments);
  else
    lastNotified = now;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net