Oleg Krupnov <mailto:oleg.krup...@gmail.com> 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 (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

Reply via email to