A function is said to be reentrant if it is accessed by more than one thread or task concurrently without any data loss or corruption.
Such function are interrupted in the middle of the execution and control returns back to it safely that is reentered again.
Such function are interrupted in the middle of the execution and control returns back to it safely that is reentered again without any data loss or corruption.
Importance of reentrant function:
This is practically very advantageous in the embedded field where memory is concern as having multiple function can occupy good amount of memory and interrupts can occur at any point of time. In such case it’s better to have reentrant function so that it can be used by more than one process/task/thread, with each function having its own copy of data.
Condition for function to be reentrant:
- It used all the shared variable in an atomic way, means it should not be interrupted.
- It should not call any function which is non reentrant.
- It should not use the hardware in non atomic way.
Thread Safe:
Any function is said to be thread safe if it can be accessed by multiple thread or task concurrently and there is no loss of data or corruption of data even though each thread or task does not have its independent copy of shared data.
In such case the data consistency is maintained by some external mechanism like mutex or semaphore.
Thread safe function are always reentrant but all reentrant functions are not thread safe.
Difference between thread safe and reentrant function:
The basic difference between thread safe and reentrant function is that in case of re-entrant function, it is termed as safe only if each thread/task uses its own copy (local variable) where as in function is said to be thread safe if is accessed by multiple threads/task and each thread does not have the independent copy of shared variable. Safety is achieved by some external mechanism like mutex or semaphore
Categories: C Language
Leave a Reply