Re: re-entering threats
Re: re-entering threats
- Subject: Re: re-entering threats
- From: Andy <email@hidden>
- Date: Thu, 15 Aug 2002 00:15:27 -0400
"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
(see you later space cowboy ...)
_______________________________________________
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.