PDA

View Full Version : What is Re-Entrant



sunil.thaha
7th January 2006, 13:43
Some one please explain
What is it not safe callin reentrant function from multiple thread ?

reentrant - Describes a function which can be called simultaneously by multiple threads when each invocation of the function references unique data. Calling a reentrant function simultaneously with the same data is not safe, and such invocations should be serialized.

munna
7th January 2006, 14:31
In the following piece of code, both functions f and g are not reentrant.




int g_var = 1;

int f ()
{
g_var = g_var + 2;
return g_var;
}

int g ()
{
return f () + 2;
}



In the above code, f depends on a global variable g_var; thus, if two processes execute it and access to g_var concurrently, then the result varies depending on the timing of the execution. Hence, f is not reentrant. Neither is g; it calls f, which is not reentrant.

Hope its clear

Cheers

katrina
7th January 2006, 17:39
In the following piece of code, both functions f and g are not reentrant.




int g_var = 1;

int f ()
{
g_var = g_var + 2;
return g_var;
}

int g ()
{
return f () + 2;
}



In the above code, jk;hfd;hw4ejkbndohf;kjlhasdjkbweuofvjknsdklbvuioh; jklhsdf;uiohjenofuijnweuojvf nkjsdhbujreuihvuo wbelh;sljkgh849ohn AZUObhn vujorhguoghv nvuhiro;gorauighnr;ogeh

Hope its clear

Cheers

Thats what it looks like to me :-)
(My fault, though, I have a hard time following things like that)

My understanding is (put REALLY simply) reentrant code is basically code that doesn't use global variables, is that sort of correct?

Katrina

jacek
7th January 2006, 18:37
My understanding is (put REALLY simply) reentrant code is basically code that doesn't use global variables, is that sort of correct?
I guess that definition should be something like:
"Reentrant code is a code that doesn't use shared resources without proper locking mechanism."

Where "shared resource" can be:
globally accessible variable or data structure,
static variable,
environment variable,
file,
and probably many other things.

Thomas
8th January 2006, 19:47
From my point of view the term shared resource is misleading. Think of a static method variable:



const int foo(void)
{
static int bar(0);

bar += 42;
return bar;
};


The variable bar is not shared among other methods but only by the method foo itself. Though, foo is not reentrant. I just wanted to clarify things. :)

jacek
8th January 2006, 20:03
The variable bar is not shared among other methods but only by the method foo itself.
One could say it's shared among all calls to that function. Anyway, you are right, static variables can cause a lot of troubles even in a single-threaded application.

wysota
8th January 2006, 20:56
As the name itself suggests, a "reentrant" function (or object or whatever) is a function (or object or whatever) which can be re-entered (entered again) when "there already is something inside" (like the flow). Hence reentrancy is important not only for multithreaded environments, but also for single threaded ones.

Example 1:


void funcA(int x){
static int var = 7;
operateOn(var);
if(conditionMet) funcB(x-1);
operateAgainOn(var);
}

void funcB(int x){
funcA(x);
}

// A call to funcA() yields a call to funcB() which calls funcA()
// again => funcA is RE-ENTERED as the same thread is
// "inside" funcA() two times at once



Example 2: every recursive function.

katrina
9th January 2006, 01:19
My grandmother had a bedroom in her house that was non-re-entrant. If you shut the door on your way out, it would automatically lock and you couldn't get back in without taking the door off the hinges or climbing through the window... I do wonder why she never just bought another door knob... (My dad had to take her door off a couple of times lol) oh well!

Katrina

wysota
9th January 2006, 02:21
That's more likely a deadlock ;) Reentrancy is about having more than one entity operating on the same object (so reentrant room is when more than one person can fit there without spreading havoc).

Thomas
10th January 2006, 00:06
That's more likely a deadlock ;) Reentrancy is about having more than one entity operating on the same object (so reentrant room is when more than one person can fit there without spreading havoc).

Hmm, I would say, reentrancy is a room with more than one door.

Dusdan
10th January 2006, 00:19
Hmm, I would say, reentrancy is a room with more than one door.nice definition, I think it fits quite well

wysota
10th January 2006, 01:34
Hmm, I would say, reentrancy is a room with more than one door.

Hmmm... I wouldn't agree... That would be two entry points, not reentrancy. Better would be to say that reentrancy is a bedroom with more than one bed. Why? Because it is not about entering the function, but about behaviour inside it. For me a room with more than one door is an example of a locking mechanism, like a semaphore.

Thomas
11th January 2006, 01:39
Exactly, wysota.

It depends on the point of view what the bedrooom and the bed are. If the bed is a kind of member in the method bedroom, the doors represent bedroom using threads. In this way, every bed would represent another member inside the bedroom. :)

wysota
11th January 2006, 01:49
The point is reentrancy is not about threads. An object may not be reentrant even in a single threaded environment.

But let's leave that, we've come waaaaay offtopic :eek: (I like that smiley...)

Thomas
11th January 2006, 04:00
I know. It was meant as an example for other objects trying to call bedroom.

sunil.thaha
11th January 2006, 07:22
Guys,
While I am going searching about this topic. I stumbled upon this link. Hope it will be useful
http://publibn.boulder.ibm.com/doc_link/en_US/a_doc_lib/aixprggd/genprogc/writing_reentrant_thread_safe_code.htm

sunil.thaha
16th February 2006, 07:42
It's a long time... Since the last post . But I would like to post the Two definitions

According to the C++ Gui programming Book

Thread-safe : A function is said to be thread-safe when it can safely be called from different threads simultaneously. If two thread-safe functions are called from different threads on the same shared data, the result is always defined. By extension, a class is said to be thread-safe when all of its functions can be called from different threads simultaneously without interfering with each other, even when operating on the same object.

Reentrant : A class is reentrant if different instances of the class can be used simultaneously in different threads. However, accessing the same reentrant object in multiple threads simultaneously is not safe,and such accesses should be protected with a mutex. Reentrant classes are marked as such in the Qt reference documentation. Typically, any C++ class that doesn t reference global or otherwise shared data is reentrant. QObject is reentrant, but none of Qt s (3.x) QObject subclasses are reentrant.