• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: How do I make one thread wait until another thread exits?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

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: Michael Ash <email@hidden>
  • Date: Thu, 12 Feb 2009 10:05:47 -0500

On Thu, Feb 12, 2009 at 4:25 AM, Oleg Krupnov <email@hidden> wrote:
> This seems a trivial question for a multi-threading app, but I haven't
> been successful in implementing this in Cocoa. I've got deadlocks and
> strange logs for seemingly no reason.
>
> Here's my problem: There is the main thread that starts a worker
> NSOperation to do some job (-[NSOperationQueue addOperation]). In case
> if the main thread is asked to start another NSOperation, it must
> cancel the current operation, *wait until it exits*, and start another
> one.
>
> What I have tried: the operation object creates and locks an NSLock
> object in its -init. When the main thread cancels the operation
> (-cancel), it does the following:
>
> // cancel
> [m_operation cancel];
> // wait until operation exits
> [[m_operation isCompletedLock] lock];
>
> // the operation object eventually checks the -isCancelled flag and
> then sends -unlock to the lock, and its thread exits.
>
> // unlock the lock
> [[m_operation isCompletedLock] unlock];
> // release
> [m_operation release];
> m_operation = nil;

Yeah, don't do this. Locks are for mutual exclusion *only*.

Use NSConditionLock, something like this:

#define OPERATION_FINISHED 1

// operation exit
[condLock lock];
[condLock unlockWithCondition:OPERATION_FINISHED];

// wait for exit
[condLock lockWhenCondition:OPERATION_FINISHED];
[condLock unlock];

Mike
_______________________________________________

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

References: 
 >How do I make one thread wait until another thread exits? (From: Oleg Krupnov <email@hidden>)

  • Prev by Date: Re: Reading packets from an audio file
  • Next by Date: Re: Garbage Collection and NSManagedObject
  • Previous by thread: Re: How do I make one thread wait until another thread exits?
  • Next by thread: [iPhone] Abort/remove all running/pending animations
  • Index(es):
    • Date
    • Thread