Reentrancy (computing) - Further Examples

Further Examples

In the following piece of C code, neither f nor g functions are reentrant.

int g_var = 1; int f { g_var = g_var + 2; return g_var; } int g { return f + 2; }

In the above, f depends on a non-constant global variable g_var; thus, if two threads execute it and access g_var concurrently, then the result varies depending on the timing of the execution. Hence, f is not reentrant. Neither is g; it calls f, which is not reentrant.

These slightly altered versions are reentrant:

int f(int i) { return i + 2; } int g(int i) { return f(i) + 2; }

In the following piece of C code, the function is thread-safe, but not reentrant.

int function { mutex_lock; ... function body ... mutex_unlock; }

In the above, function can be called by different threads without any problem. But if the function is used in a reentrant interrupt handler and a second interrupt arises inside the function, the second routine will hang forever. As interrupt servicing can disable other interrupts, the whole system could suffer.

Read more about this topic:  Reentrancy (computing)

Famous quotes containing the word examples:

    In the examples that I here bring in of what I have [read], heard, done or said, I have refrained from daring to alter even the smallest and most indifferent circumstances. My conscience falsifies not an iota; for my knowledge I cannot answer.
    Michel de Montaigne (1533–1592)

    It is hardly to be believed how spiritual reflections when mixed with a little physics can hold people’s attention and give them a livelier idea of God than do the often ill-applied examples of his wrath.
    —G.C. (Georg Christoph)