Boost logo

Boost Users :

Subject: Re: [Boost-users] boost timer with sleep
From: Rush Manbert (rush_at_[hidden])
Date: 2008-11-12 13:25:43


On Nov 12, 2008, at 12:31 AM, Bruno Lalande wrote:

>>> There is another timer implementation that works with sleep?
>>
>> In Boost? Not that I'm aware of.
>
> If I remember well, Beman Dawes plans to propose a new implementation
> of timer. Don't know if it's supposed to behave differently about that
> precise issue though.
>

I recently ran into this problem with timer and replaced it with an
implementation using the Date Time library.

My use case was to have a stale lock timeout on a lock object. The
lock object constructor takes a timeout value, expressed as
milliseconds.

I use the timeout to create a time_duration object. Then, with the
current time and the duration, I construct a time_period object that
expresses the valid lifetime of the lock object. When I check for
timeout I just see if the current time is within the time period.

At object creation:
        boost::posix_time::time_duration d (0,0,existenceTimeoutInMillisec,0);
        boost::posix_time::ptime
now(boost::posix_time::microsec_clock::local_time());
        // This defines the overall lifetime period for the lock
        m_pOverallTimePeriod.reset (new boost::posix_time::time_period (now,
d));

which records the creation time. Note that m_pOverallTimePeriod is
declared as:
        boost::scoped_ptr<boost::posix_time::time_period> m_pOverallTimePeriod;

and I use a NULL pointer value to mean that there is no timeout.

My timeout check looks like this:
        inline bool isTimedOut (void)
        {
                boost::posix_time::ptime
now(boost::posix_time::microsec_clock::local_time());
                if ( m_pOverallTimePeriod)
                {
                        return ( ! m_pOverallTimePeriod->contains (now));
                }
                else
                {
                        return false;
                }
        }

The whole point of having the lock object is to coordinate a bunch of
competing threads, and this implementation works even though the
threads get suspended.

As always, YMMV.

Best regards,
Rush


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