$linuxjunkies
>

reentrant code

also: reentrant function, async-signal-safe

Code that can be safely called multiple times concurrently or interrupted and re-invoked without losing correctness or causing data corruption.

Reentrant code is designed to handle multiple simultaneous executions—either from different processes/threads or from signal handlers interrupting the same execution flow—without conflicts or undefined behavior.

A reentrant function uses only local variables and doesn't modify shared global state. For example, a simple calculation function like int add(int a, int b) is reentrant because each call gets its own stack frame. In contrast, a function that modifies a global counter without synchronization is non-reentrant.

Reentrant code is critical in signal handlers, multithreaded programs, and interrupt handlers where execution can be suspended unexpectedly. The POSIX standard defines "async-signal-safe" functions like write() and read() that are reentrant; unsafe functions like printf() can corrupt buffers if called from a signal handler.

Related terms