Re: Thread Question
Re: Thread Question
- Subject: Re: Thread Question
- From: Brian Bergstrand <email@hidden>
- Date: Tue, 27 Apr 2004 21:14:13 -0500
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Apr 27, 2004, at 8:43 PM, Dave Keck wrote:
>
Hey,
>
>
after following along with these posts on NSTimers and NSThreads, I
>
remembered a question I've had for awhile now...
>
>
Say I have a thread, myThread, and I also have the main thread,
>
mainThread. I also have a method, myMethod:
>
>
Question 1: Is it OK to call myMethod from myThread? Is myMethod
>
within the "scope" of myMethod (assuming all this code is in the same
>
class)?
Yes, memory pointers are shared by all threads in app. You can use any
address from any thread.
>
>
Question 2: If Question #1 is "yes": is it safe to call myMethod from
>
both mainThread and myThread, possibly at the same time (if, say,
>
mainThread and myThread both had loops that called myMethod)?
No, not without using locks within myMethod. The lock(s) would have to
protect any object ivars, and any global variables.
Also, it is not safe (or you should currently assume that it is not
safe) to call any AppKit methods from anything but the main thread. If
you are using strictly Foundation methods, you should be safe for the
most part.
>
>
Question 3: If it is OK to call myMethod from both mainThread and
>
myThread: what happens when I do?
Well, if both threads enter into myMethod, they will be sharing all
class ivars and you will most likely get some type of corruption.
>
Does it call the method by the same address if I were to call it from
>
mainThread and myThread,
Yes.
>
or does each thread have its own "copy" of each method that it calls?
>
No, each thread will not have it's own "copy" of the method. The method
code is shared. Each thread will have it's own copy of any variables
you allocate on the stack, but they will share access to class ivars.
The ivars must therefore be protected with a lock.
>
Thank you very much for any insight!
>
HTH.
>
Dave
>
Brian Bergstrand <
http://www.bergstrand.org/brian/>, AIM: triryche206
PGP Key: <
http://www.bergstrand.org/brian/misc/public_key.txt>
Only two things are infinite, the universe and human stupidity, and I'm
not sure about the former. - Albert Einstein
-----BEGIN PGP SIGNATURE-----
Version: PGP 8.0.3
iQA/AwUBQI8FannR2Fu2x7aiEQKQHACdE4JpdZmtMGalZg6gQ1UxGdKSAM8An1gA
wdzKei4kVdrfFr4JmWAEQ6ks
=YXPQ
-----END PGP SIGNATURE-----
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.