Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Sleeping infinitely

31 views
Skip to first unread message

Rob Kennedy

unread,
Dec 28, 2006, 12:53:54 AM12/28/06
to
I was reading the documentation for the Sleep API function today and
noticed that it makes special mention of passing Infinite as the sleep
duration:

"A value of INFINITE indicates that the suspension should not time out."

So, if the suspension doesn't time out, how _does_ the thread resume
running?

I can see reasons for infinite waiting in some of the other
thread-suspension functions, such as WaitForSingleObject or SleepEx. In
those functions, there are other situations built into the functions
that will cause them to return. But can Sleep(Infinite) ever return? Can
the sleeping thread be signaled from some other thread to make it resume
running, either by making Sleep return, or by causing an exception to
send execution somewhere else?

If it can't, then is there any reason to call it? Wouldn't it be easier
to simply terminate the thread?

--
Rob

Remy Lebeau (TeamB)

unread,
Dec 28, 2006, 6:20:09 AM12/28/06
to

"Rob Kennedy" <m...@privacy.net> wrote in message
news:45935bc6$1...@newsgroups.borland.com...

> So, if the suspension doesn't time out, how _does_ the thread
> resume running?

It doesn't. So never pass INFINITE to Sleep() at all.

> can Sleep(Infinite) ever return?

No.

> Can the sleeping thread be signaled from some other thread
> to make it resume running

Not when Sleep(INFINITE) is used, no. SleepEx() can do that, though.

> If it can't, then is there any reason to call it?

There is never any reason to call Sleep(INFINITE).

> Wouldn't it be easier to simply terminate the thread?

No. That is a completely separate and unrelated operation.


Gambit


Jens Gruschel

unread,
Dec 28, 2006, 6:55:35 AM12/28/06
to
> "A value of INFINITE indicates that the suspension should not time out."
>
> So, if the suspension doesn't time out, how _does_ the thread resume
> running?

I think INFINITE was introduced for SleepEx, WaitForSingleObject etc.,
but to avoid a special case INFINITE can be used for Sleep, too, even if
I see no reason to use it there. After all if INFINITE (which equals
$FFFFFFFF) was not allowed for Sleep, people could still pass values
like $7FFFFFFF instead, sleeping for 24 days, which is nearly as silly
as INFINITE.

--
Jens Gruschel
http://www.pegtop.net

William Egge

unread,
Jan 2, 2007, 8:01:25 AM1/2/07
to
This is off your topic, but I am wondering if you are using a sleep function
as a delay in the execute method of your thread. TSimpleEvents are better
to use for delays because you can terminate them at will, while sleep
functions must wait X number of milliseconds. Mostly terminating on demand
is usefull for shutting down a thread without much delay.

-Bill


Reiner

unread,
Jan 7, 2007, 7:47:21 PM1/7/07
to

"William Egge" <be...@eggcentric.com> wrote in message
news:459a579f$1...@newsgroups.borland.com...

I use Sleep in the execute methods of my threads all the time. In these
cases my threads are busily working and I do Sleep(0) where appropriate. I
have other threads that do things continuously but not as importantly (like
acquiring images for display) where I do Sleeps of up to 100 between
decoding frames to keep the CPU usage low. So I'd say short Sleeps are very
useful.

Though I've never used TSimpleEvents before (probably because I mainly use
D5) they seem to be more useful in synchronizing between threads or other
asynchronous code. Mostly my threads are running until the app is closed so
waiting up to 100ms isn't a great deal (especially when acquiring and
decoding a frame may take as long as my Sleep). Though I see the usefulness
of a TSimpleEvent, I don't see how they can be used for delays.


0 new messages