Re-entrant means that the function can be interrupted at any point, and be able to correctly finish executing after the interruption, even in cases when the same function is called one or more times in the interrupted state.
The crucial part here is that the invocation of the function called in the interrupted state must finish before the original call state is restored. This is the main difference between re-entrancy and thread safety: in order to be thread-safe, a function must be able to proceed even if the interrupting invocation is unfinished before the control gets back to the original call.
That's why the second version of the swap
is re-entrant: it always leaves t
in an unchanged state upon exiting, so entering and exiting swap
in the middle of an interrupted call will not alter the global state seen by the interrupted invocation.