Re: Am I using NSConditionLock correctly?
Re: Am I using NSConditionLock correctly?
- Subject: Re: Am I using NSConditionLock correctly?
- From: jkp <email@hidden>
- Date: Mon, 10 Oct 2005 21:37:57 +0100
I had a quick peek at your blog and although i havent read it all im
not sure i understand what you are trying to do.
"So the logic is to have the worker thread set the condition to one
state when it is running and the other state when it exits."
so you want your worker thread to do this calculation and your main
thread to be notified when it is done? Condition locks are great
things, but i think you are trying to use them the wrong way around.
You want your worker thread to sit sleeping until it has some work to
do, then you want it to do its thing and to let the main thread know
when it is done.
The way you have described it, you are making your main thread sleep
on the condition lock whilst your worker does the work...blocking in
the process. the simple question i ask you is...what does this buy
you? You might as well just do the calculation on the main thread
and simplify your life! If you have the main thread sleep on the
lock whilst the worker does its work you are still blocking, so you
achieve the same thing.
I recently implemented worker class to use throughout my current
application and i learnt a lot in the process. A condition lock is a
great way to go since it allows you to have your workers sleep
efficiently and for you to exact granular control on them when you
need their services.
I think you could do with defining a set of conditions that might
apply to your thread at any point during its life cycle. Maybe
something like NOWORK, WORKPENDING, DATAPENDING etc...You can use
these to notify the worker what it needs to do. The worker can do
its thing and when it is done you have a choice of ways to send
data / notify the main thread that things are finished. My favorite
is NSObject's performSelectorOnMainThread:withObject:waitUntilDone:
method. You can use this to call a routine on the main thread and if
you want, wait until it is finished before your worker continues on
its way. This way your main thread can carry on accepting user input
etc, but each time it returns to its runloop it gets a chance to
process a pending request from the worker to perform a selector.
Another way you could do it would be to use the condition lock to
signal you have data, and maybe use a timer to check the lock from
the main thread - again, this way you dont lock the main thread, but
when the timer fires and there is data, the main thread does the
right thing. I dont like this method myself since it is basically
polling, but that would be one way you could use the condition lock
in this way if you wanted to.
These are just some thoughts and ideas, but i think you could do with
working out exactly what you want your worker thread implementation
to be capable of, and then perhaps write a small test app that proves
that it can do these things as you want. I'd also take time to
stress test the classes you write since you will be suprised what
problems can arrise that you hadnt thought of.
If its any comfort it took me 3 iterations to get a pair of classes
that do the job for me and that i am happy with. Multithreading is a
tough topic so dont be suprised if you need to go back and rehash
your design several times before you get something that works for you.
HTH
Jamie
PS - I in no way claim to be an expert on this subject, im just
sharing my experiences with you.
On 10 Oct 2005, at 06:53, Steve Weller wrote:
I am using NSConditionLock to delay the completion of a method
while another thread completes. I am not sure that I am using it
correctly, since my conditional lock hangs up the program.
In my worker thread I do [calculatingLock lock]; [calculatingLock
unlockWithCondition:YES] when calculation starts and
[calculatingLock lock]; [calculatingLock unlockWithCondition:NO]
when it stops.
In my method that depends on the worker thread stopping I do
[calculatingLock lockWhenCondition:NO]; [calculatingLock
unlockWithCondition:NO]. The idea is that it should wait for the
condition to be set to NO before continuing. It hangs at this
point. If this is all correct then my problem lies elsewhere.
More information on my blog, URL below.
--
Watch me learn Cocoa http://homepage.mac.com/bagelturf/
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40kirkconsulting.co.uk
This email sent to email@hidden
_______________________________________________
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