473,320 Members | 1,872 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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 13582
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,426 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,065 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,426 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

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

Similar topics

0
by: Martin B | last post by:
Hallo to everyone! Problem: -------- GridView Exception: reentrant call to the SetCurrentCellAddressCore function System: ------- WinXP Professional, english, .NET Framework 2.0 Beta...
23
by: Shalini Joshi | last post by:
Hi, I was trying to find out what exactly is meant by multi-thread safe code/functions etc. I couldn't find relevant information on the web and would appreciate any useful links/information on this...
2
by: TheOne | last post by:
Would anyone please point me to a list of reentrant GNU C/C++ library functions? I have searched a lot on the web for it but without any success. Man page of signal(2) has listed system calls...
9
by: TheOne | last post by:
Would anyone please point me to a list of reentrant C library functions? I want to know which C library functions are safe to use inside a signal handler across all platforms. Does GNU C library...
0
by: news.microsoft.com | last post by:
I got this error when i bind dataview to datagridview data.Datasource= dt.DataView Error: Operation is not valid because it results in a reentrant call to the SetCurrentCellAddressCore function...
1
by: jj_online | last post by:
This is not a C++ specific issue. However, I am trying to implement a readwritelock in C++ and hence would like to get help from this group. a readwritelock allows shared lock among all readers...
17
by: fmassei | last post by:
Dear all, I'm trying to put some old code in a portable, cross-platform library and I'm facing a big problem: is there a standard, platform- independent way to write a reentrant function that...
0
by: SHP | last post by:
Hi It there a way to detect non reentrant or thread unsafe function usage at compile time? Any option in gcc? thanks shekhar
12
by: Bit Byte | last post by:
I am modifying some legacy code, to make the functions reentrant. I use lots of global data (large nested structs) to pass data around quicky. The global structures are far too large (in size) for...
0
by: ANILMOURYA | last post by:
private void FlexGrdRsltConv_CellEnter(object sender, DataGridViewCellEventArgs e) { if (blnRowenter) { return;} if...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.