On Thu, Feb 12, 2009 at 11:08 AM, Oleg Krupnov <oleg.krup...@gmail.com> wrote: > Thanks, Mike. > > I assume that I need to also use > > #define OPERATION_NOT_FINISHED 0 > > and > > condLock = [[NSConditionLock alloc] initWithCondition: > OPERATION_NOT_FINISHED]; > > Right?
Strictly speaking this is unnecessary. NSConditionLock is documented to start out at condition 0. However, being more explicit is usually a good idea, so why not. > 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? You're right, I hadn't thought of this. I believe the answer is to have four conditions: 0) NOT_STARTED 1) SHOULD_QUIT 2) RUNNING 3) FINISHED When the operation starts, it does something like this: BOOL shouldQuit = NO; [condLock lock] shouldQuit = [condLock condition] == SHOULD_QUIT [condLock unlockWithCondition:RUNNING]; if(!shouldQuit) { ...do your work here... } [condLock lock]; [condLock unlockWithCondition:FINISHED]; And then when you cancel it: [operation cancel]; BOOL isRunning = NO; [condLock lock]; isRunning = [condLock condition] == RUNNING; if(!isRunning) [condLock unlockWithCondition:SHOULD_QUIT]; [condLock unlock]; if(isRunning) { [condLock lockWhenCondition:FINISHED]; [condLock unlock]; } I think that will do everything. By checking the condition lock at the top of your operation and bailing out early, it lets you cancel the operation and then not care if it will never run, or if it will run but be cancelled early on. If the operation has already started, then it goes back to the original suggestion. Mike _______________________________________________ Cocoa-dev mailing list (Cocoa-dev@lists.apple.com) 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: http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com This email sent to arch...@mail-archive.com