Re: How do I make one thread wait until another thread exits?
Re: How do I make one thread wait until another thread exits?
- Subject: Re: How do I make one thread wait until another thread exits?
- From: James Bucanek <email@hidden>
- Date: Fri, 13 Feb 2009 09:38:29 -0700
Oleg Krupnov <mailto:email@hidden> wrote (Thursday,
February 12, 2009 9:08 AM +0200):
Thanks, Mike.
I assume that I need to also use
#define OPERATION_NOT_FINISHED 0
and
condLock = [[NSConditionLock alloc] initWithCondition: OPERATION_NOT_FINISHED];
Right?
I have a doubt here, however. What if the cancel message is sent even
before the worker thread had the possibility to start? In that case,
the NSOperation may remove itself from the queue and never start its
worker thread altogether. Than the main thread will deadlock because
the condition will never be OPERATION_FINISHED. How to fix this?
A three state condition lock. I do this all the time (typed in mail):
enum {
OPERATION_INIT,
OPERATION_RUNNING,
OPERATION_FINISHED };
-- operation --
- (id)init {
...
conditionLock = [[NSConditionLock alloc] initWithCondition: OPERATION_INIT];
...
- (void)main {
[conditionLock lock];
[conditionLock unlockWithCondition:OPERATION_RUNNING];
// do stuff
[conditionLock lock];
[conditionLock unlockWithCondition:OPERATION_FINISHED];
}
-- other thread --
// to wait until the operation thread finishes
[operation->condtionLock lockWhenCondition:OPERATION_FINISHED];
[operation->condtionLock unlock];
(note that I usually encapsulate this in a
-(void)waitUntilFinished method)
James
--
James Bucanek
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please 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