Re: NSRunLoop vs. NSThread sleep

2009-04-29 Thread Alastair Houghton

On 29 Apr 2009, at 00:57, Eric Hermanson wrote:

When implementing the while-loop in the main function of an  
NSThread, is it correct to assume it is more efficient on the  
operating system to run the current run-loop until a specified date  
rather than just use NSThread sleepUntilDate to obtain the delay?


If you're talking about a secondary thread, NSThread's -sleepUntilDate  
is probably better.  But you don't even need to use that; you could  
use sleep() or nanosleep() or even select().


However...

 I ask because I don't really need a formal run loop or special  
input source as I'm just checking a shared queue for data (and the  
delay is to allow the thread to gracefully exit).


...you shouldn't be polling your shared queue, *unless* you have some  
real-time requirement that necessitates that architecture, *or* you're  
certain that, for the entire lifetime of your thread, the chances are  
that the queue will always be full by the time the thread gets to  
check it.  If you aren't sure you need to poll, you probably don't  
want to be polling.


What you should be doing instead is either using a semaphore or an  
NSConditionLock to set things up such that your thread doesn't run at  
all unless there's work to do.


 So it's simpler to code just the NSThread sleep, however, I am  
guessing that will cause me context switching performance issues and  
I'd be better off using the NSRunLoop?


If the NSRunLoop has nothing to do, I imagine it does the same thing  
anyway.


Finally, is it acceptable to use the NSThread's cancelled/ 
isCancelled mechanism to check for the thread-exit hint, rather than  
set/check a global variable in the NSThread threadDictionary (as  
Apple's documentation shows)?


That's what -cancel and -isCancelled are designed for.  That said, if  
I were implementing a thread to managed a work queue (which is what  
you're doing), I'd be inclined to override the default implementations  
so that they added an item to the work queue... that way the thread  
could block on the work queue and would wake up if someone sent - 
cancel to it.


But all of this is somewhat academic in a way, because Apple has  
handily provided NSOperationQueue for you which is probably what you  
should be using unless you have some backwards compatibility  
requirement or some other requirement that NSOperationQueue doesn't  
satisfy.


Kind regards,

Alastair.

--
http://alastairs-place.net



___

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


Re: NSRunLoop vs. NSThread sleep

2009-04-29 Thread Kyle Sluder
On Wed, Apr 29, 2009 at 6:00 AM, Alastair Houghton
alast...@alastairs-place.net wrote:
 But all of this is somewhat academic in a way, because Apple has handily
 provided NSOperationQueue for you which is probably what you should be using
 unless you have some backwards compatibility requirement or some other
 requirement that NSOperationQueue doesn't satisfy.

NSOperationQueue is broken:
http://www.mikeash.com/?page=pyblog/dont-use-nsoperationqueue.html

I am not under NDA at the moment, so I'll point out that 10.5.7 is
rumored to have fixes for NSOperationQueue.  If that bears out to be
true, then by all means run with NSOperationQueue.  If not, you might
want to hold off until Snow Leopard -- it wouldn't be the first time
that Apple engineers (or anyone else, for that matter) have tackled a
bug but missed some corner cases until the next major release.

--Kyle Sluder
___

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


Re: NSRunLoop vs. NSThread sleep

2009-04-29 Thread Alastair Houghton

On 29 Apr 2009, at 11:44, Kyle Sluder wrote:


On Wed, Apr 29, 2009 at 6:00 AM, Alastair Houghton
alast...@alastairs-place.net wrote:
But all of this is somewhat academic in a way, because Apple has  
handily
provided NSOperationQueue for you which is probably what you should  
be using
unless you have some backwards compatibility requirement or some  
other

requirement that NSOperationQueue doesn't satisfy.


NSOperationQueue is broken:
http://www.mikeash.com/?page=pyblog/dont-use-nsoperationqueue.html


Yes, it is, though I was under the impression that not everyone was  
seeing problems with it.  However, given the simpler sample program  
Mike put in the comments, it does look pretty borked to me.


Anyway, there's always

http://www.rogueamoeba.com/utm/2008/12/01/raoperationqueue-an-open-source-replacement-for-nsoperationqueue/ 



(for instance).

Kind regards,

Alastair.

--
http://alastairs-place.net



___

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


Re: NSRunLoop vs. NSThread sleep

2009-04-29 Thread Michael Ash
On Wed, Apr 29, 2009 at 6:53 AM, Alastair Houghton
alast...@alastairs-place.net wrote:
 On 29 Apr 2009, at 11:44, Kyle Sluder wrote:

 On Wed, Apr 29, 2009 at 6:00 AM, Alastair Houghton
 alast...@alastairs-place.net wrote:

 But all of this is somewhat academic in a way, because Apple has handily
 provided NSOperationQueue for you which is probably what you should be
 using
 unless you have some backwards compatibility requirement or some other
 requirement that NSOperationQueue doesn't satisfy.

 NSOperationQueue is broken:
 http://www.mikeash.com/?page=pyblog/dont-use-nsoperationqueue.html

 Yes, it is, though I was under the impression that not everyone was seeing
 problems with it.  However, given the simpler sample program Mike put in the
 comments, it does look pretty borked to me.

It appears that the reason not everybody can reproduce the problem is
because the bug is hardware-dependent. Thus this fact won't save you
unless your code won't ever escape your machine (or 10.5.7 fixes it,
or you only support 10.6 and that fixes it).

 Anyway, there's always

 http://www.rogueamoeba.com/utm/2008/12/01/raoperationqueue-an-open-source-replacement-for-nsoperationqueue/

 (for instance).

Note that although this is of course the best code ever (ahem) it is
not equally capable compared to NSOperationQueue. (It mostly does
less, but has some extra capabilities as well.) However for what the
poster is after it's probably a good fit, since it's basically a fancy
worker thread.

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


NSRunLoop vs. NSThread sleep

2009-04-28 Thread Eric Hermanson

Hello,

When implementing the while-loop in the main function of an NSThread,  
is it correct to assume it is more efficient on the operating system  
to run the current run-loop until a specified date rather than just  
use NSThread sleepUntilDate to obtain the delay?  I ask because I  
don't really need a formal run loop or special input source as I'm  
just checking a shared queue for data (and the delay is to allow the  
thread to gracefully exit).  So it's simpler to code just the NSThread  
sleep, however, I am guessing that will cause me context switching  
performance issues and I'd be better off using the NSRunLoop?


Finally, is it acceptable to use the NSThread's cancelled/isCancelled  
mechanism to check for the thread-exit hint, rather than set/check a  
global variable in the NSThread threadDictionary (as Apple's  
documentation shows)?


Thank You,
- Eric


___

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