Boost logo

Boost Users :

From: Delfin Rojas (drojas_at_[hidden])
Date: 2006-08-30 02:38:52


> Andrew Wingorodov wrote:
>
> Why result of my program such?
> auth() sleeps one second, and I have established four in timeout.
> I thought, that will be "okey".
> why timeout?
>
> <snap>
>
> ///-----------------------------
> static timed_mutex mut;
> void timeout ()
> {
> timed_mutex::scoped_timed_lock lk (mut,4);

scoped_time_lock takes boost::xtime or bool. 4 here is interpreted as
"true", not as time. To pass 4 seconds you need:

boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += 4; // <-- 4 seconds timeout for lock
timed_mutex::scoped_timed_lock lk (mut,xt);

> if ( lk.locked ())
> cout << "timeout!" << endl;
> else
> cout << "okey!" << endl;
> }
>
> timed_mutex::scoped_timed_lock *lock;
>
> void auth ()
> {
> cout << "auth" << endl;
> boost::thread::sleep ( delay(1));

I am not sure what this delay function does. I am more familiar with this
form:

boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += 1;
boost::thread::sleep(xt);

> cout << "auth end" << endl;
> delete lock;
> }
>
> int
> main ()
> {
> lock = new timed_mutex::scoped_timed_lock (mut,10);

Same as above:

boost::xtime xt;
boost::xtime_get(&xt, boost::TIME_UTC);
xt.sec += 10;
lock = new timed_mutex::scoped_timed_lock (mut, xt);

> thread_group run;
>
> run.create_thread ( &auth);
> run.create_thread ( &timeout);
> run.create_thread ( &timeout);
> run.create_thread ( &timeout);
>
> run.join_all();
> }

After you do those modifications the program works as expected.

-delfin


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