mardi 2 janvier 2018

Timer wrap-around problems

Is the following code correct

Adding an event:

pushEvent(std::chrono::steady_clock::now() + dt, event_obj);

In event loop:

//...
auto now=std::chrono::steady_clock::now();
if(event.expireTime<=now)
    {
    event.fire();
    }
//...

I am somewhat worried about wrap-around behavior, as I know of at least two bugs [1] and [2], that was introduced due to wrap-around in time counters. What I think of could happen:

  • The event fires early, since std::chrono::steady_clock::now() + dt will be a smaller than std::chrono::steady_clock::now()

  • The event fires late, since std::chrono::steady_clock::now() may have a wrap-around already.

    [1] http://ift.tt/2EGAXeT

    [2] http://ift.tt/2DRYrfO

Aucun commentaire:

Enregistrer un commentaire