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:

    No rules exist, and examples are simply life-savers answering the appeals of rules making vain attempts to exist.
    André Breton (1896–1966)

    There are many examples of women that have excelled in learning, and even in war, but this is no reason we should bring ‘em all up to Latin and Greek or else military discipline, instead of needle-work and housewifry.
    Bernard Mandeville (1670–1733)