Are static inline functions thread safe?

点点圈 提交于 2020-01-25 20:28:07

问题


Scenario: I have written a big piece of code, running on 2 parallel threads, which are identical in term of code and just process different data. I am seeing non-deterministic results. If I disable one of the 2 threads, the results become deterministic. Within this code I am using some static inline functions (main reason: they are small functions that I need here and there, for which I simply duplicate the code in the translation units where they are needed), and I would like to understand if they could be a possible cause of my problem.

Are static inline functions thread-safe in C? Said the other way around, if they have no static variable inside, but only some local variables and the input parameters, will a simultaneous call from the 2 threads cause unpredictable behaviour?


回答1:


No, that really should be fine.

Whether or not a function is inline or not shouldn't affect this, since it's all just code anyway.

Is the behaviour non-deterministic in terms of actual in-memory data (results), or "just" things like timing and ordering of events?

Do you perhaps write to the parameters, accidentally creating a data race?




回答2:


Are static inline functions thread-safe in C?

Yes. Static inlined functions have no effect on thread safety. But if you are using Static variables then you have to be careful.

if they have no static variable inside, but only some local variables and the input parameters, will a simultaneous call from the 2 threads cause unpredictable behaviour?

If you are using local variable then you will not have any issue. Because every thread have it's own stack memory.



来源:https://stackoverflow.com/questions/40125761/are-static-inline-functions-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!