How to create a timer in boost::thread
Hi, Can you please tell me if i can do this with boost::thread library? 1. create a timer (say 5 seconds) which execute a function in a different thread when it expires. 2. the creator can cancel the timer before it expires. Thank you.
I was trying to find the same in the boost lib but I did not find available in boost lib. You can use SDL lib timer from: web: http://www.libsdl.org Because it is LGPL licensed, so you don't have to expose your source when using its library. You can also extract only timer lib from its whole lib. Best regards, Fanzhe ++++++++++++++++++++++++++++ Goldiom Systems LLC - Information and Communication Technology Provider Email: fanzhe.cui@goldiom.com Tel: 973-210-4254 www.goldiom.com -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Meryl Silverburgh Sent: Saturday, June 16, 2007 7:59 PM To: boost-users@lists.boost.org Subject: [Boost-users] How to create a timer in boost::thread Hi, Can you please tell me if i can do this with boost::thread library? 1. create a timer (say 5 seconds) which execute a function in a different thread when it expires. 2. the creator can cancel the timer before it expires. Thank you. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 16/06/07, Meryl Silverburgh
Can you please tell me if i can do this with boost::thread library? 1. create a timer (say 5 seconds) which execute a function in a different thread when it expires. 2. the creator can cancel the timer before it expires.
It's not premade, but you could do it easily enough. Start a thread
which will wait (until an xtime), then call a function (stored in a
boost::function
Thanks. Can you please tell me what do you mean by 'wait (until an xtime)'?
On 6/16/07, me22
On 16/06/07, Meryl Silverburgh
wrote: Can you please tell me if i can do this with boost::thread library? 1. create a timer (say 5 seconds) which execute a function in a different thread when it expires. 2. the creator can cancel the timer before it expires.
It's not premade, but you could do it easily enough. Start a thread which will wait (until an xtime), then call a function (stored in a boost::function
perhaps), so long as a flag isn't set (you could make that in a shared_ptr<bool> to avoid lifetime worries). Starting a thread for each one probably wouldn't be too scalable, but it should work.
~ Scott _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
On 17/06/07, Meryl Silverburgh
Thanks. Can you please tell me what do you mean by 'wait (until an xtime)'?
See the thread::sleep function: http://boost.org/doc/html/boost/thread.html#id1006145-bb
me22 wrote:
See the thread::sleep function: http://boost.org/doc/html/boost/thread.html#id1006145-bb
Looking at the documentation here: http://boost.org/doc/html/boost/xtime.html I still have a couple of questions about using xtime. 1) Presumably the 'platform-specific-type' is a floating value of some sort rather than an integer? 2) What are the valid values for 'clock_type' in the function xtime_get? K
Kirit Sælensminde wrote:
me22 wrote:
See the thread::sleep function: http://boost.org/doc/html/boost/thread.html#id1006145-bb
Looking at the documentation here: http://boost.org/doc/html/boost/xtime.html I still have a couple of questions about using xtime.
1) Presumably the 'platform-specific-type' is a floating value of some sort rather than an integer?
No. For Boost.Thread (at least) it's (currently) an integer on all platforms.
2) What are the valid values for 'clock_type' in the function xtime_get?
Judging from the header, TIME_UTC seems the only valid value. BTW. is there any hope for a more usable time class in Boost.Thread, soon? Regards, Tobias
me22 wrote:
On 16/06/07, Meryl Silverburgh
wrote: Can you please tell me if i can do this with boost::thread library? 1. create a timer (say 5 seconds) which execute a function in a different thread when it expires. 2. the creator can cancel the timer before it expires.
It's not premade, but you could do it easily enough. Start a thread which will wait (until an xtime), then call a function (stored in a boost::function
perhaps), so long as a flag isn't set (you could make that in a shared_ptr<bool> to avoid lifetime worries).
That works. However, it's nicer to use a 'condition' instead of a global
flag and a 'timed_wait' on that condition to put the thread asleep.
This way the condition will wake up the thread so it can be reused.
I'll attach a simple "interval timer" that uses a condition variable to
terminate the thread. Changing it to the "stop watch timer" described in
the OP should be a matter of just adding some more conditions.
Regards,
Tobias
#include <string>
#include <iostream>
#include
You can check boost::asio (http://asio.sourceforge.net/)
Here you can find pretty simple examples for boost::asio::deadline_timer
which is (I think so) answer on both of your questions.
HTH,
Sergey
2007/6/17, Meryl Silverburgh
Hi,
Can you please tell me if i can do this with boost::thread library?
1. create a timer (say 5 seconds) which execute a function in a different thread when it expires. 2. the creator can cancel the timer before it expires.
Thank you. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
I checked asio timer before, when I was looking for boost timer library. The
smallest time granularity of asio::deadlime_timer is second. I did not find
any example or document of asio that mentioned about smaller granularity.
++++++++++++++++++++++++++++++++++++
Goldiom Systems LLC
- Information and Communication Technology Provider
Email: fanzhe.cui@goldiom.com
Tel: 973-210-4254
www.goldiom.com
From: boost-users-bounces@lists.boost.org
[mailto:boost-users-bounces@lists.boost.org] On Behalf Of Sergey Nikulov
Sent: Monday, June 18, 2007 1:16 AM
To: boost-users@lists.boost.org
Subject: Re: [Boost-users] How to create a timer in boost::thread
You can check boost::asio (http://asio.sourceforge.net/)
Here you can find pretty simple examples for boost::asio::deadline_timer
which is (I think so) answer on both of your questions.
HTH,
Sergey
2007/6/17, Meryl Silverburgh
participants (6)
-
fanzhe cui
-
Kirit Sælensminde
-
me22
-
Meryl Silverburgh
-
Sergey Nikulov
-
Tobias Schwinger