Re: re-entering threats
Re: re-entering threats
- Subject: Re: re-entering threats
- From: "Gerriet M. Denkmann" <email@hidden>
- Date: Thu, 15 Aug 2002 09:43:00 +0200
Subject: Re: re-entering threats
"Gerriet M. Denkmann" wrote:
The documentation says: "The issue of "re-entrancy" is also often
confused with thread-safety."
So very true. But even after reading the docs I am still confused.
Could some kind soul please explain the difference to me?
This is not so much a definition as an example of how they can be
different
re-entrancy: is it safe for 2 threads (or processes) to be in the same
function at the same time?
Instance variable:
int m_start;
Instance method:
- (int) notReentrant: (int) a_start {
m_start = a_start;
return 2 * m_start;
}
Consider 2 calls:
NSLog(@"value %i", [self notReentrant: 4]);
NSLog(@"value %i", [self notReentrant: 5]);
You'd expect
value 8
and
value 10
But if you ran the 2 calls on 2 threads and this happened
Thread A Thread B
1 NSLog(@"value %i", [self notReentrant: 4]);
2 NSLog(@"value %i", [self notReentrant: 5]);
3 m_start = a_start; // m_start= 4
4 m_start = a_start; //m_start =5
5 return 2 * m_start; // 10, oops!
6 return 2 * m_start; // 10...
you'd see
value 10
and
value 10
So that function is not re-entrant because it has state (global state,
or instance/class variable state in an OO language) that can be
corrupted if 2 threads enter the function at the same time. There are
lots of other ways to be non-rentrant (failing due to not handling
recursive calls well, functions which modify their own code).
Its also not thread safe.
Here's another simple example:
Instance variable:
int m_start;
Instance methods:
- (void) setStart: (int) a_start {
m_start = a_start;
}
- (int) getResult {
return 2 * m_start;
}
Now imagine 2 threads both run
Thread A Thread B
1 [self setStart: 4]
2 [self setStart: 5];
3 [self getResult]; //10 oops!
4 [self getResult];
The point is all of the methods, setStart: and getResult, are
re-entrant. It doesn't matter how many threads enter the any of the
methods, in and of itself the method always does the right thing. But
the class with setStart: and getResult, taken as a whole, is not thread
safe.
This example is so trivial (and contrived) I'm not sure if it'll help.
But look at it this way: many things that are re-entrant, turn out to be
not thread safe when you look at the bigger picture. Usually its fairly
easy to spot non-re-entrant methods - proving a class is thread safe is
harder because you have to look at the whole.
--
AndyT (lordpixel - the cat who walks through walls)
A little bigger on the inside
Thanks for excellent explanation.
Let me rephrase it, to check whether I have understood you correctly:
1. A class is thread-safe, if
a) all functions (or methods) are re-entrant and
b) there are no unwanted side effects between different methods
across ivars.
Point b) is stated in the documentation as:
"Able to have operations invoked simultaneously by multiple threads of
execution on an instance without that producing invalid results or an
inconsistent or corrupt state within the instance."
2. A function is re-entrant if it:
a) does not use global variables
b) does not modify its own code (which a normal C programm could
not easily do anyway)
c) if it handles recursive calls well (is this something I should
be concerned of, or is this taken care of by the compiler ?)
d) lots of other things (could you elaborate on this?)
What confuses me in the documentation is: "All other classes may or
may not be re-entrant.", i.e. it talks of classes (not functions) beeing
re-entrant or not.
I also states explicitly that e.g. NSNotificationCenter is both
thread-safe and re-entrant, and that NSNumber is thread-safe, but maybe
not re-entrant.
Another sentence which I cannot get the meaning of is: "Re-entrancy is
only possible where operations "call out" to other operations in the
same object or on different objects."
Could this really mean that a function (or class?) is only re-entrant if
it "calls out" to other operations?
Or does it mean: "Non-re-entrancy can only happen where operation "call
out"..." ?
And could you give me an example of such a "calling out" which makes the
class re-entrant or not?
Gerriet.
_______________________________________________
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.