How to sleep inside a boost::thread?
Hi, If I create a boost::thread like this: boost::thread thrd1(&reader); 1. how can I sleep inside the thread? (in the method 'reader') 2. when does the thread 'thrd1' terminate?
On 16/03/07, Meryl Silverburgh
Hi,
If I create a boost::thread like this:
boost::thread thrd1(&reader);
1. how can I sleep inside the thread? (in the method 'reader')
I don't know of any portable way to sleep/pause the function. One (perhaps unhelpful) option could be std::cin.get()? This would pause the thread until you hit the keyboard, although I'm unsure if any locking issues arise here (I don't _think_ they would). 2. when does the thread 'thrd1' terminate? thrd1 terminates when reader() returns. hth, Darren
On 3/15/07, Darren Garvey
On 16/03/07, Meryl Silverburgh
wrote: Hi,
If I create a boost::thread like this:
boost::thread thrd1(&reader);
1. how can I sleep inside the thread? (in the method 'reader')
I don't know of any portable way to sleep/pause the function. One (perhaps unhelpful) option could be std:: cin.get()? This would pause the thread until you hit the keyboard, although I'm unsure if any locking issues arise here (I don't _think_ they would).
Thanks. I am looking for something which I can sleep the current thread for 5 seconds.
2. when does the thread 'thrd1' terminate?
thrd1 terminates when reader() returns.
hth, Darren
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Darren Garvey escreveu:
On 16/03/07, *Meryl Silverburgh*
mailto:silverburgh.meryl@gmail.com> wrote: Hi,
If I create a boost::thread like this:
boost::thread thrd1(&reader);
1. how can I sleep inside the thread? (in the method 'reader')
I don't know of any portable way to sleep/pause the function. One (perhaps unhelpful) option could be std:: cin.get()? This would pause the thread until you hit the keyboard, although I'm unsure if any locking issues arise here (I don't _think_ they would).
You can block a thread by trying to lock an already locked mutex. If you must wake the thread when a certain condition is met by the program, just release the lock when the condition is met. If you must wake the thread after a T amount of time, use a timed lock with a T timeout. -- Pedro Lamarão
Pedro Lamarão wrote:
You can block a thread by trying to lock an already locked mutex.
If you must wake the thread when a certain condition is met by the program, just release the lock when the condition is met.
No, never do that. A mutex is not a waiting primitive, it's a serialization primitive. Use condition::wait to wait for something.
On 3/15/07, Meryl Silverburgh
boost::thread thrd1(&reader);
1. how can I sleep inside the thread? (in the method 'reader')
how about boost::thread::sleep? http://boost.org/doc/html/thread.html#id2449425-bb Note that xtime is an absolute time, not a delta, so you'll need to get current and add 5 before passing it to that.
2. when does the thread 'thrd1' terminate?
When it's done. If the thread that started it might finish before thrd1, then you'll probably want to thrd1.join() at some point. ~ Scott McMurray
participants (5)
-
Darren Garvey
-
me22
-
Meryl Silverburgh
-
Pedro Lamarão
-
Peter Dimov