Recalling past practices, I've never considered or practised using static variables for recursive functions. Except for constants.
- Re-entrant functions do not modify static variables or shared resources and do not read static non-constants or modifiable shared resources.
- Therefore, Re-entrant => thread-safe, but not vice versa.
- Recursive functions modifying static variables are non-re-entrant.
Therefore,
re-entrant recursive functions are thread-safe.
While, non-re-entrant recursive functions are not thread-safe, except when access of shared/static resources are effectively constrained by synchronisation boundaries.
Then the following question begs to be answered.
If a function modifies a database record, would that make it no longer re-entrant?
I am thinking that as long as the external resource is instantiated per entrantiation, the function is re-entrant. For example, a database record with a unique identity run-key. A new record with a new run-key is generated per entrantiation.
However, is that actually like making a static variable thread-safe? It think it is more like a thread-safe static hash-table that generates a unique key,value pair for each entrantiation and therefore no key,value pairs are shared between entrants.
So, when database records or static resources dispenses unique instances of its resources per entrantiation, a function is effectively re-entrant but I think due to its dependence on thread-safe-guarding of an external shared resource, academics might say it is thread-safe but not re-entrant.
For such an argument, I would beg to differ. For example, for a hypothetical programming language, and its specification does not restrict any of its implementation from using a shared database or a global hash to store variables. So the programmer is unaware of a thread-safe-managed resource being used underneath the implementation of the language. So the programmer goes forth and write a "re-entrant" function, or so he/she thinks. So does that make his/her "re-entrant function" non-re-entrant?
Therefore, my conclusion is, as long as the static/shared resource dispenses a unique instance per entrantiation, a function is re-entrant.
Apologies for coining the terms entrantiation/re-entrantiation, due my lack of knowledge for a better word.