🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.windows.ms
Article #2076

Re: sleep() in Windows

#2076
From: Dan Ts'o
Date: Fri, 01 Dec 1989 06:05
56 lines
1557 bytes
In article <21446@usc.edu> papa@pollux.usc.edu (Marco Papa) writes:
>Does anybody have a "quick and dirty" UNIX-like sleep() for Windows?
>If that is not the case, what would be the best way to do it? I.e.:
>
>1. Using the timer device
>2. Using the timer interrupt
>3. Using the time() routines of MS-C

>Using 1, would allow an implementation that is more "friendly" to other
>concurrent tasks.

	Below is my quick and real-dirty sleep() which I use in regular
MSC. Love to see a better one that really is friendlier and have it
really make a difference. Most DOS programs seem to busy-wait anyways.

#include <time.h>

sleep(n)
register unsigned n;
{
	register int i;
	long tm, tm1;

	time(&tm);
	tm1 = tm + n;
	while (n--)
		for (i = 0; i < 30000; i++) {
			time(&tm);
			if (tm >= tm1)
				return 0;
		}
	return -1;
}

/* Millisecond sleep */
nap(n)
long n;
{
	register int i;
	long tm, tm1;

	tm = clock();
	if (tm == -1) {
		fprintf(stderr, "Clock() cannot return ticks\n");
		return -1;
	}
	tm1 = tm + n;
	while (n--)
		for (i = 0; i < 30000; i++) {
			tm = clock();
			if (tm >= tm1)
				return 0;
		}
	return -1;
}
#endif
Technical Details
Message-ID: <865@rna.UUCP>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!rutgers!cmcl2!rna!dan
References: <21446@usc.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!cs.utexas.edu!rutgers!cmcl2!rna!dan
From: dan@rna.UUCP (Dan Ts'o)
Newsgroups: comp.windows.ms
Subject: Re: sleep() in Windows
Message-ID: <865@rna.UUCP>
Date: 1 Dec 89 06:05:29 GMT
References: <21446@usc.edu>
Reply-To: dan@rna.UUCP (The Dan of all vices)
Organization: Rockefeller University Neurobiology
Lines: 56