I have been searching for the definition and the use of reentrant function.But I couldn't understand the definition given in other web pages.If any on knows pls explain it simply?
A function is said to be reentrant if there is a provision to interrupt the function in the course of execution, service the interrupt service routine and then resume the earlier going on function, without hampering its earlier course of action. Reentrant functions are used in applications like hardware interrupt handling, recursion, etc.
Re-entrant functions have to follow certain guidelines (such as no use of static variables in C) lest different instances of them trample on each other. Show activity on this post.
For example, a function could be wrapped all around with a mutex (which avoids problems in multithreading environments), but if that function is used in an interrupt service routine, it could starve waiting for the first execution to release the mutex. The key for avoiding confusion is that reentrant refers to only one thread executing.
Here these two functions are non-reentrant. The first one is using one global variable, so it is non-reentrant. The second one is calling one non-reentrant function, so these also is not reentrant function. Now these two functions are reentrant functions.
Wikipedia has quite nice article on re-entrancy.
Function is called
reentrant
if it can be interrupted in the middle of its execution and then safely called again ("re-entered") before its previous invocations complete execution
What makes one function not being re-entrant? Check the article further, but roughly:
Do not use static or global variables in your function since those may be changed by time your function resumes
Function must not modify its own code (e.g. some low level graphic routines may have "habit" to generate itself)
Do not call any function that does not comply with the two rules above
When to use re-entrant function? Here are some examples:
Functions executed in interrupt context must be re-entrant.
Functions that will be called from multiple threads/tasks must be re-entrant.
A re-entrant function is one that can be interrupted (typically during thread context-switching), and re-entered by another thread without any ill-effect.
Functions that rely on a local variable are considered re-entrant due to the fact that their variables are safely encapsulated between threads.
Consider the case of a non-re-entrant function that uses a global variable.
Thread A is in function X, and incrementing a global variable. Thread B interrupts Thread A, enters function X and also increments the same global variable.
Thread A's behaviour and state has been changed (most likely incorrectly) by another thread and therefore the function it was in cannot be considered re-entrant.
Reentrancy is applicable in concurrent programming. A reentrant function guarantees it’s functionality even when the function is invoked (reentered) from concurrent multiple threads. Also have look on what is reentrant function?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With