Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What kind of code can be called "re-entrant"?

Could someone tell me what code can be called "re-entrant" code?

I came across this word when reading some real time operating system. What disciplines must be stuck to in order for code to be "re-entrant" code?

like image 961
smwikipedia Avatar asked Feb 16 '10 16:02

smwikipedia


1 Answers

In general, a re-entrant block of code is one that can be entered by another actor before an earlier invocation has finished, without affecting the path that the first actor would have taken through the code. That is, it is possible to re-enter the code while it's already running and still produce correct results.

In most cases, the "actors" are threads of the same process, but the concepts of thread safety and re-entrant are subtly different: not every thread-safe block is re-entrant, but every re-entrant block is thread-safe. That is, re-entrancy is a stronger property than thread safety. Here's a good example from Raymond Chen of how a block of code might be thread-safe but not re-entrant.

There's a special case when the code is recursive: the same actor is calling into the code before its own invocation is finished, as Marc Gravell points out. All correct recursive blocks are re-entrant; of course, not every re-entrant block is recursive.

like image 141
John Feminella Avatar answered Nov 18 '22 18:11

John Feminella