• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Help: while(;;) loop vs. NSTimer / NSThread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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.

  • Prev by Date: Re: retrieving your IP address
  • Next by Date: Re: Window Controller not being released.
  • Previous by thread: Re: retrieving your IP address
  • Next by thread: [ANN] Path Finder 2.0
  • Index(es):
    • Date
    • Thread