how to know when a thread is done setting up
how to know when a thread is done setting up
- Subject: how to know when a thread is done setting up
- From: "William Irving Zumwalt" <email@hidden>
- Date: Sun, 24 Dec 2006 20:25:44 -0600
I've been following along the tutorials to setup a thread and using
the threadviewer, I can see that the thread is started and also exits
when I command it to.
My thread being started from a controller like ...
[MyServer startNetworkThreadWithTag:0 forController:self];
[server start];
When 'startNetworkThreadWithTag' is called, it makes a call to another
method 'connectWithPorts' and this method notifies the controller by
calling 'setServer' and setting the 'server' var which means the
server has been setup and is ready to communicate via DO.
The problem is that 'startNetworkThreadWithTag has already returned
and called [server start] before 'connectWithPorts' has finished and
called the 'setServer' method, thereby not setting my 'server' var and
making my call [server start] meaningless.
The call [server start] is actually supposed to start my calculating
process within the thread. But all I've been able to do is start and
destroy it so far.
Any ideas how to handle this properly?
My code to setting up the server looks like this ...
+ (NSConnection *)startNetworkThreadWithTag:(int)theTag
forController:(id <NetworkController>)controller
{
// Ports used for communications between the threads.
NSPort *port1 = [NSPort port];
NSPort *port2 = [NSPort port];
// The port that is a receive port for the main thread needs to be the
// send port for the new thread and vice-versa.
NSArray *portArray = [NSArray arrayWithObjects:port2, port1,
[NSNumber numberWithInt:theTag], nil];
NSConnection *connection = [[NSConnection alloc]
initWithReceivePort:port1 sendPort:port2];
[connection setRootObject:controller];
// Our new thread.
[NSThread detachNewThreadSelector:@selector(connectWithPorts:)
toTarget:self withObject:portArray];
return connection;
}
+ (void)connectWithPorts:(NSArray *)portArray
{
BOOL local_debug = NO;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NetworkServer *instance = [[self alloc] init];
NSConnection *connection = [[NSConnection alloc]
initWithReceivePort:[portArray objectAtIndex:0]
sendPort:[portArray objectAtIndex:1]];
int serverTag = [[portArray objectAtIndex:2] intValue];
[connection setRootObject:instance];
// Notify controller that comm channels are now open.
[(id <NetworkController>)[connection rootProxy]
setServer:instance tag:serverTag];
if (local_debug) {
NSMutableDictionary *threadDictionary =
[[NSThread currentThread] threadDictionary];
NSLog(@"Thread dictionary:\n%@\n", [threadDictionary description]);
}
do {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode
beforeDate:[NSDate distantFuture]];
} while ([instance isRunning]);
[[connection receivePort] invalidate];
[[connection sendPort] invalidate];
[connection release];
[instance release];
[pool release];
}
_______________________________________________
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