Re: NSLock locking order;
Re: NSLock locking order;
- Subject: Re: NSLock locking order;
- From: Shawn Erickson <email@hidden>
- Date: Mon, 14 Nov 2005 08:37:20 -0800
On Nov 14, 2005, at 8:19 AM, Alastair Houghton wrote:
On 10 Nov 2005, at 23:30, Matt Budd (Madentec) wrote:
Hey Joseph,
I can't do the first suggestion, because that implies that the
worker thread is being turned on sporadically to do some work. It
is the opposite case, the worker thread is constantly evaluating
these numbers and accumulations, and only sporadically the main
thread is going to interrupt him and tell him to either accumulate
in a different direction or to start also accumulating additional
numbers. The the worker thread is going all the time, I just want
to stop it when I know there is an state update going to happen.
Hi Matt,
If the worker thread is going all the time anyway, then just use a
volatile boolean variable and test it at convenient points in the
worker thread's loop. Then when your main thread wants to make a
change and needs to stop the worker thread, have it set the
variable and try to lock a condition lock. The worker thread can
then unlock the same condition lock and then try to lock it
again... e.g.
/* -- Declarations (probably within an object instance) -- */
NSConditionLock *condLock;
volatile BOOL needsToStop;
In this example you don't even need to make "needsToStop" volatile.
You are calling functions / sending messages in the thread's while
loop so the compiler has to assume that "needsToStop" can change as a
side effect of those. It will be reloaded from memory as needed.
You really only need volatile if the value can change, affecting what
you want to do with it, while in a block of code without any function
calls / memory barriers taking place. For example the var is a memory
mapped PCI device hardware register.
Granted in this case defining it volatile will have little
performance impact.
-Shawn
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden