timed_mutex::scoped_timed_lock
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? # ./a auth auth end timeout! timeout! timeout! # ///----------------------------- static timed_mutex mut; void timeout () { timed_mutex::scoped_timed_lock lk (mut,4); 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)); cout << "auth end" << endl; delete lock; } int main () { lock = new timed_mutex::scoped_timed_lock (mut,10); thread_group run; run.create_thread ( &auth); run.create_thread ( &timeout); run.create_thread ( &timeout); run.create_thread ( &timeout); run.join_all(); } -- andr.ru
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
participants (3)
-
Andrew Wingorodov
-
Delfin Rojas
-
Jens Theisen