Re: [newbie:] elegant way using multithreading with NSThreads
Re: [newbie:] elegant way using multithreading with NSThreads
- Subject: Re: [newbie:] elegant way using multithreading with NSThreads
- From: Wolfgang Maehr <email@hidden>
- Date: Sun, 13 Mar 2005 18:59:41 +0100
Hi,
Thanks for the hints, the error seems to come in fact from my use of
NSProxy, now running without it seems to go better. Even if the
threading is not totally clean yet.
But there is still some issues unclear to me:
1) No. But you can use Distributed Objects to call methods that run in
other threads. However, each thread will need its own NSRunLoop to
process the calls, which may or may not fit your current model. You
don't need to use NSProxy directly, DO automatically creates the proxy
objects for you.
Ok, I can use my own object - no need for the NSProxy. So I just create
this object and when calling [NSThread detachNewThreadSelector:...] on
an object method, the running environment automatically splits the
objects and lets me use the proxy? Do you know if this proxy has a
message queue, or what happens with sent messages that aren't received
yet, do they get lost?
2) Wherever two or more threads may compete for a resource, you need a
mutex (and maybe a conditional lock to help).
This does not include the objects, right? I mean when sending a message
to the tracker object (in thread 1&2) from the controller object (in
thread 1) I don't need a mutex? Just when accessing tracker members in
methods that can be called in both threads, then I need a mutex?
3) No. But it's trivial to write. There is lots of code on
cocoadev.com that could help. For example:
http://cocoadev.com/index.pl?ThreadWorker
Japp, thanks for that hint, the ThreadWorker seems useful and I should
have a more detailed look at that and I guess that I'll use a similar
mechanism.
Cheers,
_Wolf
On Mar 13, 2005, at 17:44, Agent M wrote:
From your description, it sounds like you are doing everything
correctly. What you should do is use the debugger to try to identify
what causes the crash. Are you sure it is the threading issue? To
answer your questions:
1) No. But you can use Distributed Objects to call methods that run in
other threads. However, each thread will need its own NSRunLoop to
process the calls, which may or may not fit your current model. You
don't need to use NSProxy directly, DO automatically creates the proxy
objects for you.
2) Wherever two or more threads may compete for a resource, you need a
mutex (and maybe a conditional lock to help).
3) No. But it's trivial to write. There is lots of code on
cocoadev.com that could help. For example:
http://cocoadev.com/index.pl?ThreadWorker
might be useful to you. Also, pthreads can have "private" data regions
with pthread_key_* methods. Since objects aren't associated with a
particular thread by default, you could write a class that does map a
pthread to itself. I'm not sure that is useful though.
4) "OO multi-threading" isn't any different from regular
multithreading. The Cocoa thread calls are just wrappers around
pthread functions. You just need the right mindset. For example:
int *integer;
//get mutex before accessing integer
NSArray *array;
//get mutex before accessing array
There really is no difference but the NSLock,etc. classes cut down on
the C code you would otherwise need to write. In your code, it might
make sense to use a dispatcher controller through which all program
state changes are verified and then sent to new or old threads.
Good luck with your project.
On Mar 12, 2005, at 11:12 PM, Wolfgang Maehr wrote:
...
¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬ ¬
AgentM
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