• 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: Yielding an NSThread-started thread
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Yielding an NSThread-started thread


  • Subject: Re: Yielding an NSThread-started thread
  • From: Alastair Houghton <email@hidden>
  • Date: Wed, 22 Nov 2006 00:54:02 +0000

On 22 Nov 2006, at 00:27, Philip Q wrote:

On 22/11/06, Alastair Houghton <email@hidden> wrote:
Generally speaking, however, you wouldn't want to do that; it's much
better to signal a thread explicitly using a condition variable, a
mutex or some other kind of synchronisable object.  Is there some
reason that you can't do that in your case?

In this case, I'm signalling a thread to exit, but while it wraps the other thread has nothing to do but wait for it, so instead of continually polling the condition variable it might as well yield after each check to give the other thread the chance to do something.

Ah. You seem to misunderstand the term "condition variable". A condition variable is a special type of synchronisation primitive, which you can signal from one thread and wait on from another. It doesn't mean a boolean flag variable that you test each time around a loop.


In Cocoa, you probably want to use NSConditionLock for this kind of application. That way you don't have any busy waiting going on. You'd do something like:

// In the main thread (I'm assuming here that the main thread should block
// until the worker thread has completely terminated; you could write things
// so that the main thread gets called back when the worker has actually finished)
[myLock lock];
[myLock unlockWithCondition:1]; // Signal the worker thread
[myLock lockWhenCondition:0]; // Wait for worker thread
[myLock unlock]


 -------

  // In the worker thread (probably inside some sort of loop)
  if ([myLock tryLockWhenCondition:1]) {
    // Make ready to exit
    ...
    // Unlock the condition variable
    [myLock unlockWithCondition:0];  // Signal the main thread
    // Exit
    return;
  }

You *could* use a boolean flag variable to actually signal the worker thread, rather than using -tryLockWhenCondition:, but if you're avoiding synchronisation primitives you really need to know what you're doing.

Also, in production code you should probably #define some constants rather than using 1 and 0 like I have here.

Kind regards,

Alastair.

--
http://alastairs-place.net


_______________________________________________

Cocoa-dev mailing list      (email@hidden)

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: Re: Yielding an NSThread-started thread
      • From: "Philip Q" <email@hidden>
References: 
 >Yielding an NSThread-started thread (From: "Philip Q" <email@hidden>)
 >Re: Yielding an NSThread-started thread (From: Alastair Houghton <email@hidden>)
 >Re: Re: Yielding an NSThread-started thread (From: "Philip Q" <email@hidden>)

  • Prev by Date: Re: Yielding an NSThread-started thread
  • Next by Date: Re: External kill of an NSThread?
  • Previous by thread: Re: Re: Yielding an NSThread-started thread
  • Next by thread: Re: Re: Yielding an NSThread-started thread
  • Index(es):
    • Date
    • Thread