Click here to Skip to main content
15,877,966 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello guys, I've searched online , some say static functions are safe and others say the opposite. I'm quite confused.

When thinking logically , they should be safe. But I need to be sure.


The function I'm talking about doesn't have interact with any static variable. It just takes 2 parameters and manipulate them.

Here is a simplified example:

What I have tried:

C++
static void copyOneToTheOther(Class1& c1, Class2& c2) 
{
 c1.intvariable = c2.intvariable;
 c1.stringvariable = c2.stringvariable;
}



it's more or less this simple.
What do you guys think?
Posted
Updated 1-Jul-23 19:52pm

No, static functions are not inherently thread-safe.

Even your simple example isn't. Assuming both intvariable and stringvariable are supposed to be updated at the same time, another thread could observe the state of c1 between the two assignments, leading to data corruption.

For example, imagine two threads, one copying c2 to c1, and the other copying c1 to c2:

Initial state:
c1: (42, "The answer")
c2: (13, "Lucky number")

Thread 1 starts:
c1.intvariable = c2.intvariable
c1: (13, "The answer")
c2: (13, "Lucky number")

Thread 2 runs:
c2.intvariable = c1.intvariable
c1: (13, "The answer")
c2: (13, "Lucky number")

c2.stringvariable = c1.stringvariable
c1: (13, "The answer")
c2: (13, "The answer")

Thread 1 resumes:
c1.stringvariable = c2.stringvariable
c1: (13, "The answer")
c2: (13, "The answer")

Your state is now corrupt for both variables.
 
Share this answer
 
Comments
Weird Japanese Shows 12-Nov-21 4:52am    
Well my bad, seems like I didn't give enough details. I'm going to use that function only to initialize a newly created class.
To be more specific, I need to create a struct that represents a row of database in an rpg game.
When a new client is created and it logs in , I read data from database and need to assign each item in that row to appropriate variable in client_struct. So , that function cannot be called with same variables from different threads.


P.s. the reason I need the function to be static is , the exact same function is needed on server class as well as client class. So I don't want to have duplicate functions in different classes if possible.
Richard Deeming 12-Nov-21 4:56am    
If there's no shared state, then that specific function is (probably) thread-safe.

But my answer still stands: static functions are not inherently thread-safe.

I think some of the confusion may come from some boilerplate text that Microsoft inserted into their .NET Framework documentation, which read something like "static members of this class are thread-safe" - that confused a lot of people, and I think they've mostly removed it now.
Weird Japanese Shows 12-Nov-21 5:02am    
Thanks alot
Stefan_Lang 13-Nov-21 7:45am    
If you only need this to initialize newly created objects the function should create the object, or, better, you should write a constructor that takes a Class2 object for initialization:
class Class1 {
int intVariable;
std::string stringVariable;
public:
Class1(const Class2& c2)
: intVariable(c2.intVariable), stringVariable(c2.stringVariable) {}
};
Weird Japanese Shows 13-Nov-21 9:08am    
that makes sense but class1 is actually a struct and I don't want to populate the struct definition anymore. Decided to use it as a static function.
Hi, @Richard Deeming

"The method is thread-safe" or "The method is not thread-safe" - I think the point is very different here.
Since there is no state maintened here (in this class, in this method),The method is thread-safe.
Although the parameter pass in it maybe not thread-safe, it will be the caller's problem.
How caller handle the parameter is not thread-safe.

To me, if there is an thread-unsafe issue related to this method.
The root cause is not this method, the caller side should be fixed.
 
Share this answer
 
Comments
Graeme_Grant 2-Jul-23 4:53am    
Did you check the dates on the thread? Almost 2 years old and the solution was accepted. Better to focus on questions where help is wanted then answer dead questions.
Richard Deeming 5-Jul-23 4:54am    
Even ignoring the date of the question, your answer makes no sense. The question was "Are static methods inherently thread-safe", to which the only correct answer is "no", as I explained two years ago.

Whilst specific methods can be written to be thread-safe, making a method static does not automatically make it thread-safe.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900