[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: How to terminate another thread?
From: |
David Ayers |
Subject: |
Re: How to terminate another thread? |
Date: |
Tue, 23 Jul 2002 18:31:41 +0200 |
Hello Stefan,
I'm not quite sure if I understand what you need but maybe if you clairify a
couple ot things I might be able to help. But let me try anyway:
In the code you provided you seem to give EVERY Object the ability to supply a
proxy of itself running (accetping messages) in a different thread! Wow, ahm
are sure you want to do that? Ehm. Ok, well first to the question:
What you seem to be doing with proxyInNewThread is to allow any object to start
a new thread, register itself back in the thread which started it to update the
global dictionary (hopfully you only use proxyInNewThread in the one specific
thread othewise you sould guard your global dictionary) and then run the
runloop indefinatly. Now if I interpret your question correctly, you want the
runloop to stop procesing, once there are no more messages to process (input
sources).
Well I think all you have to do is use a different way of running the runloop
in _establishThreadConnectionUsingParameters:
use runUntilDate: providing a maximum timeout (at worst [NSDate distantFuture])
and this should return once all messages are processed and the thread shoud
continue (and exit) normally. You might need some additional mechanism to keep
it running until the first message has arrived, but I'm not sure on that.
BUT! I believe what you are doing is EXTREMLY dangerous. Especially if you
think of using this proxy for arbitrary objects (Or why is it a category of
NSObject?) Make sure all access to your instance variabels of the objects you
are "running" in the new thread are guarded corrctly by locks.
Nicola pointed me to a very interesting uRL about Double-Checked-Locking. I'll
forwarded privatly once I get back home. I can't access it from where I am
right now.
I prefer to use seperate Objects in a DO environment to actually insure that an
instance can only execute messages in one thread. Actually I go so far as to
have instances of different classes on either end of an connection. To set them
up I pretty much follow the docs
http://developer.apple.com/techpubs/macosx/Cocoa/TasksAndConcepts/ProgrammingTopics/DistrObjects/index.html
by passing the ports in a dictionary to a class method with is invoked by the
detach, which then allocs/inits the "Remote Object". Then the "Remote Object"
can then ask for a proxy of an object in the orignal thread if it needs it.
Then it remains clear which implemtations are executed in which thread and
locking can be avioded since the instance variables aren't shared by the
threads. But maybe you have reason not do this. And remember to guard globals
in anycase. (And as have just recently realized, static local variables must be
guarded just as globals in this case.)
Hope that helps.
Cheers,
Dave