Re: Pausing an NSThread
Re: Pausing an NSThread
- Subject: Re: Pausing an NSThread
- From: Ron Fleckner <email@hidden>
- Date: Sun, 8 Nov 2009 14:47:15 +1100
On 08/11/2009, at 2:36 PM, Dave DeLong wrote:
Instead of a BOOL on the main thread, what about an NSLock? Start
off by locking it on the main thread, and then the secondary thread
can try to lock it, block (because it can't acquire the lock since
the main thread has it), and not resume until the main thread
unlocks it (equivalent to threadPaused = NO). The only thing is
that your thread method would need to accept both the arrayOfObjects
and the lock, unless you were to make the lock somehow globally
available.
With regards to your code, the myLock variable you have is totally
worthless. Locks are used to help coordinate inter-thread
communication, but in your code, only one thread has myLock. So it
locking and unlocking it does nothing except eat cpu cycles.
HTH,
Dave
Aha... now I'm beginning to see the light. I see also, thanks to
Kyle, that I need to do some (or a lot of) general background research
on threading and locks and what it all means. Thanks very much to
both of you.
On 08/11/2009, at 2:36 PM, Kyle Sluder wrote:
On Sat, Nov 7, 2009 at 7:28 PM, Ron Fleckner <email@hidden
> wrote:
The CPU usage goes down to zero according to Activity Monitor while
the thread is 'paused'. That's gotta be a good sign.
No, it just means you didn't screw up in an obvious way. It doesn't
mean you're free of race conditions, corner cases, or other bugs you
haven't tripped on yet.
It would behoove you to read a good introduction to multithreading
that explains condition locks and other threading primitives. The
Cocoa docs expect you to have this foundation already. Unfortunately,
I don't have a link handy. Mind you, even the best tutorial won't
save you from the fact that multithreaded programming is very
difficult, and quite often doesn't get you the results you're looking
for even when done correctly. This is one reason why the Cocoa APIs
favor runloop-based asynchrony over multithreading.
--Kyle Sluder
_______________________________________________
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