Is it necessary to threadsafe a static method?

KevinMU1

Senior member
Sep 23, 2001
673
0
0
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
 

EagleKeeper

Discussion Club Moderator<br>Elite Member
Staff member
Oct 30, 2000
42,589
5
0
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.
 

singh

Golden Member
Jul 5, 2001
1,449
0
0
Short Answer: No

Make sure you use only variables allocated on the stack (or dynamically allocated ones).
 

KevinMU1

Senior member
Sep 23, 2001
673
0
0
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.