• 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
How do I make one thread wait until another thread exits?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

How do I make one thread wait until another thread exits?


  • Subject: How do I make one thread wait until another thread exits?
  • From: Oleg Krupnov <email@hidden>
  • Date: Thu, 12 Feb 2009 11:25:29 +0200

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;

The effect is that sometimes it works but sometimes not, and I get
logs in the console:

*** -[NSLock lock]: deadlock (<NSLock: 0x197a90> '(null)')
*** -[NSLock unlock]: lock (<NSLock: 0x197a90> '(null)') unlocked from
thread which did not lock it
*** Break on _NSLockError() to debug.

Is this a valid way of implementing the subject? Am I using NSLock
correctly for this purpose? Or if not, can someone please post the
correct way of doing this?
_______________________________________________

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

  • Follow-Ups:
    • Re: How do I make one thread wait until another thread exits?
      • From: Michael Ash <email@hidden>
    • Re: How do I make one thread wait until another thread exits?
      • From: Andrew Farmer <email@hidden>
  • Prev by Date: Re: Paste with no Edit Menu
  • Next by Date: [iPhone] Abort/remove all running/pending animations
  • Previous by thread: Cocoa application won't die
  • Next by thread: Re: How do I make one thread wait until another thread exits?
  • Index(es):
    • Date
    • Thread