Subject: Re: sleep, usleep, and nanosleep vs. pthreads?
To: Florian Stoehr <netbsd@wolfnode.de>
From: Rui Paulo <rpaulo@NetBSD.org>
List: netbsd-users
Date: 09/17/2005 23:54:07
--17pEHd4RhPHOinZp
Content-Type: text/plain; charset=us-ascii
Content-Disposition: inline
Content-Transfer-Encoding: quoted-printable

On 2005.09.18 00:50:36 +0200, Florian Stoehr wrote:
| On Sat, 17 Sep 2005, Steven M. Bellovin wrote:
|=20
| >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, http://www.cs.columbia.edu/~smb
| >
|=20
| From the manpage of usleep(3):
|=20
| The microseconds argument must be less than 1,000,000.  If the value=20
| of microseconds is 0, then the call has no effect.
|=20
| It should set errno to EINVAL then.

Yes, this works fine:

#include <pthread.h>
#include <unistd.h>

void *
pth_routine(void *arg)
{
	usleep(100000);
	return 0;
}


int
main(int argc, char *argv[])
{
	pthread_t pth;
=09
	pthread_create(&pth, NULL, pth_routine, NULL);
	pthread_join(pth, NULL);
}


		-- Rui Paulo

--17pEHd4RhPHOinZp
Content-Type: application/pgp-signature
Content-Disposition: inline

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (NetBSD)

iD8DBQFDLJ6PZPqyxs9FH4QRAlYmAJ0Y/pnK22g3nbuKwTBhGugiSaezCACeLxev
SUiwFmrLPA0AKNCwR99OFlk=
=aThR
-----END PGP SIGNATURE-----

--17pEHd4RhPHOinZp--