469,356 Members | 2,043 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,356 developers. It's quick & easy.

What is Reentrant function?

8
I had some information from internet but Iím unable to get the meaning.

Please find the below mentioned description and please let me know if (common English) are u able to get.

Must hold no static (or global) non-constant data.
True only if shared variable changes address during execution or when program is executed again. Absolute addresses of constant globals are safe unless they are changed from "outside".
Aug 5 '09 #1
8 13193
JosAH
11,448 Expert 8TB
Google knows the answer.

kind regards,

Jos
Aug 5 '09 #2
kp7796
8
Can you please give the complete meaning? I’m not able understand the meaning of the discretion.
Aug 12 '09 #3
donbock
2,422 Expert 2GB
Can you please explain what you don't understand about the definition JosAH pointed to?

I suggest this discussion will be more efficient if you try to tell us what a reentrant function is. We can then correct any errors and fill in any gaps.
Aug 12 '09 #4
kp7796
8
Functions should be writing any global variable than only the function called as “reentrant”.
Global variable is used in a function but variable value is not modified its read only this function “not reentrant”
Please let me know what is the meaning of mush hold no static and global why they are mentioned “Must hold no static (or global) non-constant data”
Aug 12 '09 #5
kp7796
8
1. Must hold no static (or global) non-constant data.



int GLOBAL /* define Global variable */

int Function 1(void)

{

int local ; /* define local variable */

local= GLOBAL; /* read the global variable */

local+=1;

}



int Function 2 (void)

{

GLOBAL+=1; /* modifying the value of global variable */

}



The function reentrancy is depends on

Single thread environment ,the function 1 irrespective of accessing global variable in read mode is termed as reentrant
Multiple thread environment, there is a possibility that Value of “GLOBAL” may be overrun by parallel execution of function1 and function2 and hence function 1 called as Non reentrant
Can you please let me know my understating is right or wrong?
Aug 12 '09 #6
kp7796
8
1. Must hold no static (or global) non-constant data.

int GLOBAL /* define Global variable */
int Function 1(void)
{
int local ; /* define local variable */
local= GLOBAL; /* read the global variable */
local+=1;
}

int Function 2 (void)
{
GLOBAL+=1; /* modifying the value of global variable */
}

Can you please let me know above functions are reentrant or non-reentrant if reentrant please give me reasons.
Aug 12 '09 #7
Banfa
9,064 Expert Mod 8TB
Actually re-entrancy is only a issue in a program that has more than one thread of execution (either multiple treads or a thread with interrupt routines or multiple treads with interrupt routines).

In a single thread environment re-entrancy is not an issue since the is only 1 thread of execution only 1 instance of the function can be running at any time.

In a multiple threaded environment then accessing non-constant global data makes a thread non-re-entrant. That is both your functions Function1 and Function2 are not re-entrant because the access the non-constant global variable GLOBAL.

The non-constant part is important because if the variable is a constant then it can not be changed so it always has the same effect on function execution so this is re-entrant.

Expand|Select|Wrap|Line Numbers
  1. const int GLOBAL = 5;  /* define constant Global variable */
  2. int Function1(void)
  3. {
  4.     int local ; /* define local variable */
  5.     local = GLOBAL; /* read the global variable */
  6.     local+=1;
  7.     return local;
  8. }
  9.  
Another alternative is to pass the data that is making the function not re-entrant so this is re-enrant

Expand|Select|Wrap|Line Numbers
  1. int GLOBAL = 5;  /* define Global variable */
  2.  
  3. int Function1(int input_value)
  4. {
  5.     int local ; /* define local variable */
  6.     local = input_value; /* read the local parameter */
  7.     local+=1;
  8.     return local;
  9. }
  10.  
  11. /* Some other code elsewhere */
  12.  
  13. result = Function1(GLOBAL);
  14.  
Function1 is no re-entrant as it only works on local variables and parameters, however th place calling Function1 is not re-entrant as it uses the global variable GLOBAL.


The real test of re-entrancy is are you access data that a different portion of the program (including the same function running in a different thread) might be changing at the same time?
Aug 12 '09 #8
donbock
2,422 Expert 2GB
Another term you might run into is "thread-safe". It refers to whether it is safe to run a particular function in multiple threads. Banfa's link explains the subtle distinction between thread-safety and reentrancy. Evfery reentrant function is also thread-safe; but a thread-safe function is not necessarily reentrant.

As Banfa said, the real criteria for whether a function is reentrant is whether there are any portions of it where improper operation could occur if multiple instances of the function were running at once. These portions of the function are sometimes referred to as "critical regions".

For example, Function1 reads a global variable and returns that value plus one without changing the global variable. Multiple copies of Function1 will not interfere with each other -- the function appears to be reentrant. However, Function2 increments that same global variable. If Function1 and Function2 run simultaneously then it is possible for Function2 to increment the global after it has been read by Function1, but before Function1 returns. Does this make Function1 nonreentrant? It depends on the consequences of Function1 returning an out-of-date value. I suggest there is no critical region in Function1; the critical region is actually in Function1's callers, starting from just before the call to Function1 and extending to where it is finished using the return value from Function1.

For example, Function2 increments a global variable. The compiler might implement this operation by reading the global variable into a register, incrementing the register, and writing the new value back to the global variable. If so, then another instance of Function2 could interfere with itself if it occurs after the read operation, but before the write operation. The effect would be to nullify the second instance of Function2. This problem can be avoided if the compiler can be coerced into implementing the increment operation via an "atomically" (for example, via read-modify-write. Unfortunately, there is no portable way to force the compiler to do this; if for no other reason, because some processors have no support for read-modify-write. Even if you can increment the global variable atomically, you have to look at the callers of Function2 to see if they would be adversely affected by multiple Function2 threads.
Aug 12 '09 #9

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

23 posts views Thread by Shalini Joshi | last post: by
2 posts views Thread by TheOne | last post: by
9 posts views Thread by TheOne | last post: by
1 post views Thread by jj_online | last post: by
17 posts views Thread by fmassei | last post: by
reply views Thread by SHP | last post: by
12 posts views Thread by Bit Byte | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.