Re: Delaying a method return
Re: Delaying a method return
- Subject: Re: Delaying a method return
- From: Jonathon Mah <email@hidden>
- Date: Mon, 10 Oct 2005 15:02:55 +0930
Hi Steve,
On 2005-10-08, at 15:06, Steve Weller wrote:
I have a method that returns some data. There is a worker thread
that could be modifying that data, so I call a stopCalculation
method that tells the other thread to stop and exit. However the
worker thread could take a little while to stop since it polls the
variable set by stopCalculation every so often. I know when it has
actually stopped because it calls a didStop delegate method. This
works and is protected against race conditions by a lock.
My question is, after telling the worker thread to stop, how do I
wait for didStop?
Perhaps you could use something like this (typed into Mail):
- (void)doWork
{
// Runs in another thread
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
[workingLock lock]; // workingLock is an instance variable of
type NSLock
while (!stopCalculation)
// ... do work
[workingLock unlock];
[pool release];
}
- (id)otherMethod
{
if (![lock tryLock])
[workerThread stopCalculation];
[workingLock lock];
// ...
[workingLock unlock];
}
So the -[NSLock tryLock] method may be what you're looking for.
Jonathon Mah
email@hidden
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden