Hello,

I see this term poping up sometimes in programming docs ... I believe it also has to do with thread safeness ...
Can anyone explain it?

yaa
Posted on 2004-01-07 06:43:50 by yaa
The ability of a function to call itself safely (or to be run simultaneously by two threads), but also I have seen it in device-drivers - there are functions/callbacks, when you must not use interrupts or some functions that actually called the currently revised function.
If you read/write some data that is static (not stack-based), the function isn't safe to be called from another thread.
Posted on 2004-01-07 06:52:14 by Ultrano

Hello,

I see this term poping up sometimes in programming docs ... I believe it also has to do with thread safeness ...
Can anyone explain it?

yaa


It means, quite literally, that a piece of code can be "reentered" while it's already executing. This happens, for example, when a function gets interrupted and the code that executes during the interrupt decides to call the same function that was interrupted.

There are several facets that define reentrancy, but the main one is that the function does not manipulate global (static) variables and it preserves everything in sight.
Cheers,
Randy Hyde
Posted on 2004-01-07 10:41:18 by rhyde

Reentrant and Recursive Subprograms
A reentrant subprogram must satisfy the following properties:

? It must not modify any code instructions. In a high level language
this would be dificult, but in assembly it is not hard for a program to
try to modify its own code. For example:

mov word , 5 ; copy 5 into the word 7 bytes ahead
add ax, 2 ; previous statement changes 2 to 5!

This code would work in real mode, but in protected mode operating
systems the code segment is marked as read only. When the first line
above executes the program will be aborted on these systems. This
type of programming is bad for many reasons. It is confusing, hard to
maintain and does not allow code sharing (see below).

? It must not modify global data (such as data in the data and the bss
segments). All variables are stored on the stack.
There are several advantages to writing reentrant code.

? A reentrant subprogram can be called recursively.

? A reentrant program can be shared by multiple processes. On many
multi-tasking operating systems, if there are multiple instances of a
program running, only one copy of the code is in memory. Shared
libraries and DLL?s (Dynamic Link Libraries) use this idea as well.

? Reentrant subprograms work much better in multi-threaded (5) programs.
Windows 9x/NT and most UNIX-like operating systems (Solaris,
Linux, etc.) support multi-threaded programs.

(5) A multi-threaded program has multiple threads of execution. That is, the program
itself is multi-tasked.

Recursive subprograms
These types of subprograms call themselves. The recursion can be either
direct or indirect.
...


Have a nice day or night.
Posted on 2004-01-07 12:21:40 by rea