X-Spam-Check-By: sourceware.org Date: Mon, 26 Feb 2007 09:29:46 -0600 From: Brian Ford Reply-To: cygwin AT cygwin DOT com To: cygwin AT cygwin DOT com Subject: Re: does usleep() sleep more than it's supposed to? In-Reply-To: <45E292D6.30906@gmail.com> Message-ID: References: <45E292D6 DOT 30906 AT gmail DOT com> MIME-Version: 1.0 Content-Type: TEXT/PLAIN; charset=US-ASCII X-IsSubscribed: yes Mailing-List: contact cygwin-help AT cygwin DOT com; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: cygwin-owner AT cygwin DOT com Mail-Followup-To: cygwin AT cygwin DOT com Delivered-To: mailing list cygwin AT cygwin DOT com On Mon, 26 Feb 2007, Carlo Florendo wrote: > I'm writing an application that requires time precisions up to the > microsecond level. Then you shouldn't be using Windows. Millisecond resolution is all the scheduler can do (this varies slightly depending on the platform). > However, I put a hard-coded adjustment of 9000 microseconds since > usleep() seems to sleep on the average of 9000 microseconds more than > it's supposed to, at least on my system. Follow this thread for history and discussion: http://sourceware.org/ml/cygwin-developers/2005-11/msg00000.html Put this in its own (separate from all Cygwin code) object file, call it before any timing calls in your application, and link it with -lwinmm: #include "windows.h" void SetSchedulerMaxRes(void) { TIMECAPS tc; /* Set the system scheduler resolution to its maximum. * Needed for Cygwin >= 1.5.20, broken in 1.5.19, and unnecessary * <= 1.5.18 because it was always done by Cygwin. Required for */ if (timeGetDevCaps(&tc, sizeof(tc)) != TIMERR_NOERROR) { printf("timeGetDevCaps error %d\n", GetLastError()); tc.wPeriodMin = 1; /* Try 1 ms and hope for the best */ } if (timeBeginPeriod(tc.wPeriodMin) != TIMERR_NOERROR) printf("timeBeginPeriod error %d\n", GetLastError()); } -- Brian Ford Lead Realtime Software Engineer VITAL - Visual Simulation Systems FlightSafety International the best safety device in any aircraft is a well-trained crew... -- Unsubscribe info: http://cygwin.com/ml/#unsubscribe-simple Problem reports: http://cygwin.com/problems.html Documentation: http://cygwin.com/docs.html FAQ: http://cygwin.com/faq/