Re: Main Thread stoppin Background Thread
Re: Main Thread stoppin Background Thread
- Subject: Re: Main Thread stoppin Background Thread
- From: Jason Coco <email@hidden>
- Date: Fri, 28 Nov 2008 15:06:35 -0500
On Nov 28, 2008, at 14:48 , John Love wrote:
Reference page 43 of Apple's MultiThreading.pdf.
I conclude from this page that the preferred, if not only, way to
have the main thread stop the background thread is through KVC.
Before I continue to inflict more pain on myself, why can't I simply
toggle the value of an instance variable of the class' interface
from the main thread and have the backgroundThreadRoutine look at
the same variable and break out of the background thread if its a
certain value?
I have been totally unsuccessful in this simpler-to-understand
approach; therefore, I am beginning to study KVC and its use on page
43 of Apple's doc.
Should I conclude that this class variable is in the main thread and
is not accessible by the background thread?
That totally depends... this is one of the big issues surrounding
multithreading. Unless the variable is marked as volatile, however, it
may be
cached and, therefore, the background thread sees a stale value.
You can use a simple mach port signal mechanism for inter-thread
communication. The main thread can hold an array (or whatever)
containing write ports
on all the background threads. In the simplest solutions, when it
wants the background threads to die, it signals them using this port.
In a more complex solution (should the need arise in the future), you
could have the main thread send commands across these ports (simple
things, like suspend, resume, die, etc). The nice thing about the mach
ports is that they're relatively easy to use (especially when you're
just using them as a signaling mechanism) and they're low-overhead.
There is a lot of documentation on Apple's website about setting this
up. If you have trouble finding it or understanding it, let me know
and I (or others on the list) can help you with specifics/examples,
whatever.
HTH, Jason
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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