user34537
user34537

Reputation:

How do i sync variables across threads? C++

Lets say i have A) Global variable B) Local variable but another thread has a ptr to it

Lets say i have this code

thread2(&localVar);//now thread2 can modify it
localVar=0;
globalVar=0;
while(1){
    mutex.lock();
    cout << (globalVar && localVar ? "Both true" : "fail");
    mutex.unlock();
    Sleep(1000)
}

Is this correct and safe? I can't remember. If it is my question is how does C++ know that localVar and globalVar may have been modified? If you say its because of mutex lock/unlock then my question is why? When calling any functions does C++ believe variables may have been modified and need to be reloaded into the register?

If this isn't safe then what makes it unsafe? (I suspect if it isnt then only localVar is unsafe), how do i correct it?

Upvotes: 0

Views: 2617

Answers (3)

Dmitry Poroh
Dmitry Poroh

Reputation: 3825

I think that first time your get address of the local variable it become volatile and all reads and writes is performing with the memory.

Upvotes: 0

Richard Sitze
Richard Sitze

Reputation: 8463

It this correct and safe?

No.

Fix: Initialize localVar and globalVar before creating your second thread.

That you've exposed a localVar (on local stack? local var suggests on stack in scope) to another thread is troublesome. If this localVar is on the stack, then just be sure that you don't let it drop out of scope.

Are you using the same mutex on the other thread, to protect read/write access? If yes, then this is moving towards safe.

It's not a question of "does C++ know that localVar and globalVar may have been modified?": C++ doesn't know. For this situation, as described, it's all about using the mutex to properly protect all access to all shared values. The mutex, if used by both threads, allows only ONE thread at a time to get past it, into the protected code. Any other thread trying to lock the mutex will "stop & wait" until the lock is released.

So you're on the right track by surrounding your reads of the pair of values by the mutex.

When calling any functions does C++ believe variables may have been modified and need to be reloaded into the register?

As a general rule, yes.

Upvotes: 0

curiousguy
curiousguy

Reputation: 8268

You are making a lot more complicated then it really is. Your responsibility is to make sure no conflicting accesses to a variable ever happen.

Definition: two accesses to a given memory location conflict iff they can happen concurrently and at least one is a write access.

So:

  • two writes to the same memory location = bad
  • a read and a write to the same memory location = bad

To ensure this never happens, you can use mutex, or you design your program so that variables shared between threads are only read. There are many other possible strategies.

If I understand your incomplete code correctly: localVar is modified concurrently in two threads. The behavior is not defined. Anything can happen.

Upvotes: 2

Related Questions