top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

What is a re-entrant function of C/C++?

+1 vote
633 views

I know some kind of re-entrant function but would like to be precise so please help with example C/C++ code?

posted Jul 6, 2015 by Pardeep Kohli

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

2 Answers

+1 vote

Reentrancy is applicable in concurrent programming. Cooperative scheduling need not to consider reentrancy. Prior to discussing on reentrant functions, we need to understand few keywords. Read atomicity and critical section posts.

If one thread tries to change the value of shared data at the same time as another thread tries to read the value, the result is not predictable. We call it as race condition.

A reentrant function guarantees it’s functionality even when the function is invoked (reentered) from concurrent multiple threads. If the function is using variables with static extent, the results will be unpredictable because of race conditions. A library developer need to care in writing reentrant code. Sometimes the bug lies in the library rather than programmer’s code. It is not easy to recreate such bugs during fix.

A Reentrant Function shall satisfy the following conditions,

Should not call another non-reentrant function

Should not access static life time variables (static/extern)

Should not include self modifying code

Thread safety and Reentrant functions

Thread safety and reentrant functions are connected. Every thread safe function is reentrant, but the converse need not be true. A thread safe function can be called from multiple threads even when the function accessing shared data. A thread safe function is guaranteed to serialize accessing any shared data.

An example is string class (C++). Most implementations of string class are reentrant but not thread safe. One can create different instances of string class across multiple threads, but can’t access same instance atomically from multiple threads. For more details of string class see QString.

Reentrancy is key while writing critical sections, signal handlers, interrupt service routines, etc…

Reentrant Function vs Functions with Reentrancy:

The above statement may look strange. There are different memory models of processors. Some architectures use a common set of memory locations to pass arguments to functions. In such case the functions can’t maintain reentrancy when invoked multiple times. Code development for such processors must qualify the function prototype with compiler provided keywords to ensure reentrancy.

answer Jul 8, 2015 by Mohammed Hussain
+1 vote

If you are using multi threaded applications and/or signal handlers shared between various threads and events you will need to uuse re-entrancy concepts. Some of them are just plain programming guidelines while doing multi threaded apps, but here are the
The general rules for writing re-entrant function are :-

a) Stay away from global variables and do not use static variables in the re-entrant functions

b) Try to avoid locks in re-entrant functions. If locks are really needed, then it may change the flow.

c) Use only the parameters which are supplied by the caller ...Also these parameters should be pass-by-value. Make sure that the function should be stateless - use only locals in the function/ return values should be copies of data rather than pointers to data / and it should also call only stateless function

There is an ibm article on this topic : http://www.ibm.com/developerworks/linux/library/l-reent/index.html

you might want to have a look at it.

answer Jul 8, 2015 by Sudeep Gopal
Similar Questions
+1 vote

I know the basic definition of re-entrant code and but don't known when it is really required ?

+1 vote

Can you please explain with examples. Also like to know if all re-entrant code is thread safe.

...