Streaming security with C ++ and passing by reference

I wanted to confirm my understanding of threads and pass by reference to C ++. Is the next stream of functions safe?

QString sA = "hello";
QString sB = "world";
bool someFlag = AreStringsEqual(sA,sB);

...

bool AreStringsEqual(QString const &stringA, QString const &stringB)
{
    if(stringA == stringB)
    {   return true;   }

    return false;
}

I think it is thread safe. I would like someone to be able to confirm my thought process or tell me that I have no idea what I'm talking about :)

  • There are two copies of sA and sB in the process memory. One set is created on the Thread1 stack, and a second set is created on the Thread2 stack. Since we followed the link, each thread needs only one set of sA and sB in memory to make a function call.

  • If we crossed by value, in the process memory (each thread with two sets) there can be up to four copies sA and sB (each thread with two sets) at some point in time when both threads traded the control processor inside the function call.

  • , .

, , :)

+5
5

, sA sB. , , , sA sB. , , , . , , , . .

. , "" ( , ). AreStringsEqual . - ( - ) , .

+2

, .

, if(stringA == stringB) . stringA string B.

stringA == stringB == 2.

stringA, , stringA, stringB 3. stringB. false ( 2 != 3), stringA stringB.

+3

QString , operator== , . AreStringsEqual .

. , (, ), AreStringsEqual. , . , QString. std::string =)

, . , - (, ). (, std::string std::vector) , . , , , .

, AreStringsEqual ( , bool QString::operator==(const QString&) const ).

AreStringsEqual:

QString sA = "hello";
QString sB = "world";
bool someFlag = AreStringsEqual(sA,sB);

, .

+2

, sA sB .

, AreStringsEqual sA sB , Race.

, .

pass by value, .

+1

, , , .

Perhaps this is thread safety based on the context that you described, but just looking at the function, it is not thread safety, because by the time the if condition is met, the string values ​​can be changed.

0
source

All Articles