Subject: Re: sleep, usleep, and nanosleep vs. pthreads?
To: Florian Stoehr <>
From: Steven M. Bellovin <>
List: netbsd-users
Date: 09/17/2005 18:58:20
In message <>, Florian Stoehr write
>On Sat, 17 Sep 2005, Steven M. Bellovin wrote:
>> I'm trying to get gtkpod working on NetBSD.  When I ran it, it went
>> into an CPU-chewing loop in the following code:
>> /* keep space_ipod_free/used updated in regular intervals */
>> static gpointer th_space_thread (gpointer gp)
>> {
>>    for (;;)
>>    {
>>        usleep (SPACE_TIMEOUT*1000);
>>        if (!space_uptodate)   th_space_update ();
>>    }
>> }
>> Contrary to the usleep call -- SPACE_TIMEOUT is 4000, so it should have
>> been a 4-second sleep -- it seemed to return either immediately or
>> virtually immediately.  Changing it to use nanosleep didn't help.
>> However, using sleep() made it work.
>> I'm puzzled.  However, I know nothing of pthreads, so I don't know if
>> some special invocation should have been used by gtkpod to make that
>> work.  The problem with usleep occurs on 2.0 and -current; the fix
>> works on -current and hasn't been tried yet on 2.0.
>> 		--Steven M. Bellovin,
>>From the manpage of usleep(3):
The microseconds argument must be less than 1,000,000.  If the value 
>of microseconds is 0, then the call has no effect.
>It should set errno to EINVAL then.
Clearly pilot error on my part in not seeing that (and a worse error on 
the part of the original programmer for doing it...)  Any thoughts on 
why nanosleep() failed?  (Hmm -- maybe I should try it again, and make 
sure I got it right...)

		--Steven M. Bellovin,