I make the following reasoning, please tell me what’s wrong (or right) about it:
‘If inlining a function duplicates the code in the place the function is called, then the static and local variables are duplicated for each function calling it and if there is only one thread running the function that calls the inlined one at the same time, then the code is thread-safe’.
‘And, if it doesn’t help with static and global variables, does it with code that is creating temporary variables?’
Thanks
When you declare a function as inline, it is merely a hint to the compiler. Static variables have a clear definition in the language. If the compiler does inline the function, it is still obligated to keep the static variables shared between all instances of the function. Therefore, they will remain global and have to be protected in a MT environment.
As to the local variables, unless they are used outside of the function, they are thread safe regardless of the function being inlined or not.