Help: while(;;) loop vs. NSTimer / NSThread
Help: while(;;) loop vs. NSTimer / NSThread
- Subject: Help: while(;;) loop vs. NSTimer / NSThread
- From: Hisaoki Nishida <email@hidden>
- Date: Tue, 22 Oct 2002 23:37:02 -0400
I thought I knew how threads worked, but I guess I don't.
Any help appreciated.
I have some code that originally ran in a while(;;) loop and basically
kept listening for incoming connections. I realized it would be better
if I used a thread for this method.
So I did:
(In my delegate class)
[NSThread detachNewThreadSelector:@selector(threadedRun:)
toTarget:server
withObject:nil];
(In my server class)
- (void)threadedRun:(id)newObject
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
while(YES)
{
[self run];
[NSThread sleepUntilDate:[NSDate
dateWithTimeIntervalSinceNow:1.0]];
}
[pool release];
}
Or even if I simply used an NSTimer:
timer = [[NSTimer scheduledTimerWithTimeInterval:1.0
target:server selector:@selector(run) userInfo:nil repeats:YES]
retain];
In BOTH cases I get this weird error (that I don't get if I just used a
while(;;) loop.
In my - (void)run method this is where the error occurs:
if(i == [serverSocket socketID])
serverSocket is declared in my server class header, along with a bunch
of global variables I use in - (void)run. serverSocket right before
this if statement is in tact. It's not nil or anything. But when I
access socketID it crashes with a signal 11 (SIGSEGV)!
-(int)socketID just simply returns an int, a socket file descriptor.
Again, this error doesn't show up if I used a while(;;) loop instead of
a timer or a thread, my server code works perfectly.
Could there be something about timers or threads I am unaware of?
Thank you.
-Yuki
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.