Re: How to set scheduling in NSThread based application
Re: How to set scheduling in NSThread based application
- Subject: Re: How to set scheduling in NSThread based application
- From: "Shawn Erickson" <email@hidden>
- Date: Mon, 12 Feb 2007 14:34:24 -0800
On 2/12/07, Gilles Celli <email@hidden> wrote:
Shawn,
Thanks for your quick reply.
To get the data from the multimeter a 'READ' command is sent to it
every 0.5seconds.
I'm polling with gettimeofday() the time, and sleeping with a
nanosleep of 250000000L after the acquisition, something like this:
I wonder in fact if this is the right way to do it.... so please
don't laugh at my hobbyist-programming horror code ;-)
So here comes the first execution after detaching the first NSThread:
// Start and loop the acquisition until the user clicked the button
'Disconnect'
while ( threadIsRunning ) {
gettimeofday(&tval, NULL);
if ( ( tval.tv_usec == SECOND_ZERO ) || (tval.tv_usec ==
HALF_SECONDS_IN_MICROSEC ) )
Ah. I think you are being a little to strict in the check here. You
basically are saying that in order to capture a sample from the device
gettimeofday has to be exactly some particular value down to
microsecond accuracy (which isn't assured by anything you are doing in
your thread... and in practice hard to get).
Consider something like the following...
#define JITTER = 1000 * 20 // 20 ms
if ( ((tval.tv_usec < labs(SECOND_ZERO + JITTER )) &&
(tval.tv_usec > labs(SECOND_ZERO - JITTER ))) ||
((tval.tv_usec < labs(HALF_SECONDS_IN_MICROSEC + JITTER )) &&
(tval.tv_usec < labs(HALF_SECONDS_IN_MICROSEC - JITTER )))
This allows you to capture at 0.5 second intervals +/- 20
milliseconds. Also maybe change the logic so that if you haven't
captured and more then 0.5 has passed force a capture... unless you
are trying to drop samples that aren't close to the 0.5 sample time.
Also I would consider calculating about how long you want to sleep
right before you ask for the thread to sleep (minus maybe half your
JITTER allowance). This implies calling gettimeofday again and doing a
little math.
Personally I would run a runloop in this capture thread with a timer
set to fire at 0.5 second intervals (with the timing calculated from a
base start time to avoid build of skew) ...but maybe you really want
to capture based on system clock time stamps.
-Shawn
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden