what is reentrant function or code explain with examples
A function or code or subroutine is called reentrant if it can be called by many tasks simultaneously without any conflict. It is clear from the term “re-entrant” that it is safe to called a function or subroutine while it is already executed.A function is not re-entrant when the execution by one task has an impact on the influence of another task. This typically is the case when a global state or data is used.
what makes a function reentrant?
- The reentrant function or code can be called by many tasks but there would be only a single copy of function in the system.
- A single copy of function or code executed by many tasks is known as shared code of function and it must comply with reentrant property.
- reentrant function must hold non static or non global non constant data.
- Must not return the address to static (or global) non-constant data.
- Must not modify its own code.
- Must not call non-reentrant computer programs or routines.
What is Reentrant function and Thread safe function
In the case developing multithread application one should know the basic difference between Reentrancy and Thread safety. A thread-safe function is always reentrant, but a reentrant function is not always thread-safe because
- A thread-safe function may use shared data and can be called simultaneously by many tasks if the shared data is serialized.
- A reentrant function can also be called simultaneously from many tasks, but only if each invocation uses its own data.