PDA

View Full Version : A way to make a thread sleep



mnguyen
15th December 2004, 03:02 AM
Hi guys,

I'm working on my school project using C/C++ languages. There is a module in which I need to do tasks simultaneously. So I am using the POSIX thread (pthread library) available in the libc.

My problem is : sometimes, i want my thread to suspend an amount of time (say 500ms). The current function I am using is sleep(). However, this function suspends the thread in term of "second" (I remember that the sleep() function of Java works in term of millisecond). Do you know any way to force my thread to suspend in term of milisecond ? (not using a loop since it would keep the CPU busy)

james_in_denver
15th December 2004, 03:42 AM
Looks like you can use "getitimer", throw an exception or a signal when the timer expires, and either catch the exception, or trap the signal......

rogue
15th December 2004, 06:36 AM
The usleep() (http://www.opengroup.org/onlinepubs/007908799/xsh/usleep.html) function will suspend thread execution for the specified number of microseconds... with all of the signals and interrupts that go on, I wouldn't count on any degree of accuracy though.

Real Time Linux (http://www.realtimelinuxfoundation.org/) is meant to overcome that limitation of the kernel, but last I heard Linus rejected all of the patches. It was too ambitious for 2.6 stable but maybe RT is something to look for in 2.7?