Reentrancy, reentrant function

Reentrancy, reentrant function

A computer program or routine is described as reentrant if it can be safely called again before its previous invocation has been completed (i.e it can be safely executed concurrently).

To be reentrant, a computer program or routine:

* Must hold no static (or global) non-constant data.

* Must not return the address to static (or global) non-constant data.

* Must work only on the data provided to it by the caller.

* Must not call non-reentrant computer programs or routines.

A really clean reentrant package will require no additional RAM beyond the stack and/or heap.

The standard library functions (malloc() and free()) are not normally reentrant, which would be problematic in a multithreaded application.If source code is available, this should be straightforward to rectify by locking resources using  semaphore , mutex etc.

example of a re-entrant function:

int power(int x, int count)
{
if (count<=0) return(1);
return(x*power(x, count-1));
}

Suppose we modify the function as follows:

double power(double x)
{
if (count<=0)return(1);
–count;
return(x*power(x));
}
where exp is now defined as a global variable, accessible to many other functions. The function still works correctly. However, if this function was called and then an interrupt which calls the same function came along while it is executing, it will return an incorrect result. The variable count is fixed in memory; it is not unique to each call, and is therefore shared between callers, a disaster in an interrupting or multi-threading environment .

Thread-safe vs Re-entrant :

An operation is “thread-safe” if it can be performed from multiple threads safely, even if the calls happen simultaneously on multiple threads.

An operation is re-entrant if it can be performed while the operation is already in progress (perhaps in another context). This is a stronger concept than thread-safety, because the second attempt to perform the operation can even come from within the same thread

Resolving technical problems:

Solve your technical problems instantly

We provide Remote Technical Support from Monday to Sunday, 7:00PM to 1:00 AM

Mail your problem details at writeulearn@gmail.com along with your mobile numberand we will give you a call for further details. We usually attend your problems within 60 minutes and solve it in maximum 2 days.

4 thoughts on “Reentrancy, reentrant function”

  1. When I originally commented I clicked the -Notify me when new feedback are added- checkbox and now every time a comment is added I get four emails with the identical comment. Is there any manner you may remove me from that service? Thanks!

  2. Hi Ovl
    Let me take it in further details:
    I am not using any global variable in first example :
    int power(int x, int count)
    {
    if (count<=0) return(1);
    return(x*power(x, count-1));
    }

    but using a global variable count in second example:
    int count =0;
    double power(double x)
    {
    if (count<=0)return(1);
    –count;
    return(x*power(x));
    }

    This makes first example as re-entrant , since if its called by one thread ,all its member will be stored in its own stack frame and even if thread switch and it gets called again in some another thread , it will have a different stack frame , so output for each thread would be independent of the other thread.

    But in second example, count modified by one thread can get used by another thread which that thread may not expect and hence it may get an unexpected outcome. So , second function is not re-entrant.

    Hope , this comment would clear doubts.

Leave a Reply

Your email address will not be published. Required fields are marked *