9

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?

adhi .b
  • 311
  • 1
  • 3
  • 9
  • 4
    [What exactly is a reentrant function?](https://stackoverflow.com/questions/2799023/what-exactly-is-a-reentrant-function). If you don't understand it then you need to tell us precisely what you don't understand. Otherwise we can't really target any new/different way of explaining it if we don't know why you don't understand all the existing explanations. – kaylum Jan 13 '16 at 05:19
  • 1
    Maybe you should refer to http://stackoverflow.com/questions/2799023/what-exactly-is-a-reentrant-function – Marvin Wang Jan 13 '16 at 05:19

5 Answers5

16

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.

Box Box Box Box
  • 5,094
  • 10
  • 49
  • 67
Ivan Angelov
  • 313
  • 1
  • 7
  • I understood the definition now.Where we have to use this type of function ?. – adhi .b Jan 13 '16 at 06:10
  • 2
    Functions executed in interrupt context must be re-entrant. Functions that will be called from multiple threads/tasks must be re-entrant too. – Ivan Angelov Jan 14 '16 at 01:12
  • thanks now I understood where we have to use this . – adhi .b Jan 21 '16 at 04:44
  • Uhm, there are other ways of achieving thread-safety without reentrant functions... https://www.quora.com/When-is-a-function-reentrant-How-does-that-relate-to-it-being-thread-safe If reentrant functions were the only possibility, that would mean you would not be allowed to use global variables from multiple threads, no matter what and that is simply not the case. – DragonGamer Dec 27 '17 at 02:23
  • A code example of a reentrant function would be useful. – Goaler444 Sep 12 '19 at 17:32
8

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.

Martin Konecny
  • 57,827
  • 19
  • 139
  • 159
3

Put at its simplest (or as simple as I can make it, a re-entrant function is one that can be re-entered before it's actually finished executing.

One typical situation is with interrupts, where you may be in the middle of a function when an interrupt occurs, then the interrupt service routine calls that function as part of its workload.

Another is with recursion, where a function calls itself (either directly or indirectly).

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
3

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?

Community
  • 1
  • 1
Mohan
  • 1,871
  • 21
  • 34
1

A re-entrant function is a function which can be called safely in a threaded or interruptible environment. it usually means it has no reliance on variables or properly uses mutexes to prevent accessing the same data object at the same time.

So the issue is this. If you call a function and it sets up some variable. you then call that same function before the first call is complete from another thread (or interrupt) the new call will destroy the variables you have setup. When the interrupting thread finishes your original function resumes but now has different data. Thus it fails.

In a nutshell re-entrant functions protect against this type of problem

DeveloperChris
  • 3,412
  • 2
  • 24
  • 39