|
Boost Users : |
Subject: Re: [Boost-users] boost::interprocess::interprocess_semaphore::timed_wait - how to do a relative wait
From: Viatcheslav.Sysoltsev_at_[hidden]
Date: 2011-02-24 05:03:25
On Thu, 17 Feb 2011 16:02:06 +0100, Jean-Sebastien Stoezel
<js.stoezel_at_[hidden]> wrote:
> Hi:
>
> I won't provide an example, just either do a sleep, or call a timed wait
> on
> a semaphore, mutex etc... with a long enough timeout (say 60s). While
> your
> thread is waiting, change the clock of your computer backwards by say 2h.
> Your thread will be waiting 2h instead of 60s.
>
Well, I tried the following test program:
#include <boost/date_time/posix_time/posix_time.hpp>
#include <boost/interprocess/sync/interprocess_semaphore.hpp>
int main()
{
struct timespec td_start,td_end;
boost::interprocess::interprocess_semaphore sem(0);
boost::posix_time::ptime
t(boost::posix_time::microsec_clock::universal_time());
printf("Start waiting\n");
clock_gettime(CLOCK_MONOTONIC, &td_start);
bool ret = sem.timed_wait(t + boost::posix_time::seconds(30));
clock_gettime(CLOCK_MONOTONIC, &td_end);
printf("Finished waiting %s, waited %d seconds\n", ret == false ? "due
to timeout" : "due to some other semaphore event",
td_end.tv_sec-td_start.tv_sec);
return 0;
}
compiled with gcc 4.3.2 on SUSE 11.1 (usual x86) against boost 1.45, it
happens to run just fine:
vsysolts:~/test/boost (1) > g++ -g -O0 -I/home/vsysolts/boost/boost_1_45_0
-L/home/vsysolts/boost/boost_1_45_0/stage/lib test2.cpp -lboost_date_time
-lpthread && a.out
Start waiting
Finished waiting due to timeout, waited 30 seconds
I have set the system clock 5 minutes back while it was waiting. It has
waited for around 30 seconds, definitely not for about 5 minutes.
Btw the native semaphores are not affected by system time on my system too.
So what are you objecting against *exactly*?
-- Slava
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