IT lexicon › Programming › Reentrancy

Reentrancy

Programming På svenska → Updated: 2026-11-03

The property of code that can be interrupted halfway through and called again before the first call finishes, without either run trampling the other's data.

In practice it means the function touches only locals and its arguments. Static buffers, global state and calls into non-reentrant functions all break it. The textbook offender is C's strtok, which remembers its position in a static pointer; call it from both the main loop and an interrupt handler and each mangles the other's string. Hence strtok_r.

In embedded work the question is concrete, because an interrupt can land between any two instructions. Newlib handles its own share with a per-task _reent structure that the RTOS swaps on every context switch. Note that reentrant and thread-safe are not the same thing: a function guarded by a mutex is thread-safe, but call it from an interrupt that preempted the lock holder and it deadlocks for good.

← Back to the lexicon