Click here to Skip to main content
15,615,787 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 11-Nov-21 22:27pm

1 solution

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.

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