calls to select seem to mess with NSTimers..
calls to select seem to mess with NSTimers..
- Subject: calls to select seem to mess with NSTimers..
- From: Charles Bennett <email@hidden>
- Date: Fri, 26 Oct 2001 16:51:14 -0400
Hi All, I been tracking down an odd situation where a NSTimer seemed to be
keeping "bad" time.
(10.1, 568M ram, G5 500)
The symptom's are this..
Start a non-repeating timer set to go off in 3 minutes.
Works great.
Start a repeating timer to go off once each second for three minutes.
ack... took 5 minutes to get to count to 180..
Humm.
I tracked this down to an unrelated piece of code making calls to select.
It seems that some amount of time spent in the select call is being lost
each timer repeat BUT a single run of the timer works..
Oddly the lost time seems to be almost exactly 1 second every other timeout..
I created a test example where there are two repeating timers
one timer calls a routine with a "select" that WILL timeout in two
seconds because it has stdin open and there is no input ready.
The other timer is just a one second "printer"
the one second timer looses time like crazy..
Here is some output.. notice that the first three seconds are great
(until the 3 second timer starts) then the one second timer starts loosing time
like crazy.
Is this a bug or am I doing something that I "shouldn't"?
2001-10-26 16:45:08.889 NSTimerTest[7236] oneSecond timeout
2001-10-26 16:45:09.823 NSTimerTest[7236] oneSecond timeout
2001-10-26 16:45:10.823 NSTimerTest[7236] oneSecond timeout
2001-10-26 16:45:12.826 NSTimerTest[7236] oneSecond timeout <lost one second
2001-10-26 16:45:13.824 NSTimerTest[7236] oneSecond timeout
2001-10-26 16:45:15.827 NSTimerTest[7236] oneSecond timeout <lost one second
2001-10-26 16:45:16.825 NSTimerTest[7236] oneSecond timeout
2001-10-26 16:45:18.828 NSTimerTest[7236] oneSecond timeout <lost one second
2001-10-26 16:45:19.827 NSTimerTest[7236] oneSecond timeout
notice the pattern?
chuck
#import "MainController.h"
#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#define SEC 1000000L
@implementation MainController
static int ttyPtr = -1;
//true == DID timeout
//false = did not timeout (ok)
- (BOOL) waitforinput {
int flag;
struct timeval tm;
fd_set portList;
unsigned long timeout = 2*SEC;
if( ttyPtr == -1) {
ttyPtr = open("/dev/stdin",O_RDONLY);
if( ttyPtr < 0 ) NSLog(@"it didn't open\n");
exit(0);
}
/* wait for character to become present */
FD_ZERO(&portList);
FD_SET(ttyPtr, &portList);
tm.tv_sec = timeout / SEC;
tm.tv_usec = timeout % SEC;
flag = select(ttyPtr + 1, &portList, 0, 0, &tm);
if( flag == 0) return TRUE;
if( flag > 0 ) return FALSE;
return TRUE;
}
-(void) pollTimeout {
BOOL ok = [self waitforinput];
//NSLog(@"%@\n",(ok) ? @"TIMEOUT" : @"OK");
}
- (void) oneSecondTimeout {
NSLog(@"oneSecond timeout\n");
}
- (void)applicationDidFinishLaunching:(NSNotification *)notification {
oneSecondTimer = [NSTimer scheduledTimerWithTimeInterval:1
target:self
selector:@selector(oneSecondTimeout)
userInfo:(id)nil
repeats:TRUE];
pollTimer = [NSTimer scheduledTimerWithTimeInterval:3
target:self
selector:@selector(pollTimeout)
userInfo:(id)nil
repeats:TRUE];
}
@end
[demime 0.98b removed an attachment of type application/x-pkcs7-signature which had a name of smime.p7s]