Search code examples
crefactoringreentrancy

Making a non-reentrant function reentrant


I am using plain old c.

I have a function that uses static local variables and is therefore non-reentrant. I would like to remove the use of the static locals and make the function reentrant.

Any tips or design recommendations for how to do this?

Is this question too vague? Does it require more details about the design I'm currently working with? Or are there general design patterns that can be followed in this sort of situation?


Solution

  • Instead of using static locals to maintain state, have the caller pass those state variables to your function as parameters. It means your caller will have to maintain state, but that's usually what you want anyway.