'C++ assignment operator in while true loop and in thread doesn't work
I have a thread which has while true loop and every cycle I'm assigning global variable to local variable and for some reason it doesn't work
void Loop()
{
while (true)
{
std::unique_ptr<Data> data = std::make_unique<Data>();
Mutex->Sync->lock();
*data = *globalData;
Mutex->Sync->unlock();
//doing stuff
}
}
Another loop which assigns data to global variable
void Loop2()
{
while (true)
{
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::unique_ptr<Data> data = std::make_unique<Data>();
//doing stuff
data->fpsCamera = fpsCamera;
data->localGameWorld = localGameWorld;
Mutex->Sync->lock();
*globalData = *data;
Mutex->Sync->unlock();
}
}
I start the loop in main()
std::thread th(Loop);
th.detach();
std::thread th2(Loop2);
th2.detach();
That's what debugger says (My breakpoint is set AFTER assignment) image 1 image 2
EDIT: If I assign like that then everything works correctly:
data->fpsCamera = globalData->fpsCamera;
data->localGameWorld = globalData->localGameWorld;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
