• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Is it necessary to threadsafe a static method?

KevinMU1

Senior member
Hi,

I have a C++ program where I have multiple threads that could possibly call the same static member function of a different class at the same time. My class has no static member variables--the static method uses only the parameters passed in. Is it necessary to threadsafe this code with critical sections or mutexes to prevent different threads from clobbering each other or is the static method inherently threadsafed?

Thanks for any advice.

Kevin
 
If you have no references to static variables and any references to external data are controlled by parameters passed in or are protected by other locking methods, you may not be completely safe.

References to external data that depend on parameters passed in could be affected if multiple calls to the static object are made from different threads using a same paramter.
 
Short Answer: No

Make sure you use only variables allocated on the stack (or dynamically allocated ones).
 
Well it looks like I've done OK with the static methods but it's taken some time to work out lock problems in the actual threaded code. How I love threads!

Thanks for your help everyone.
 
Back
Top