KaiserBreath
Senior Member
- Joined
- Feb 22, 2005
- Messages
- 1,034
- Reaction score
- 218
Hi all,
given the following code:
********************************
using ThreadVector = std::vector<std::thread>;
ThreadVector tv;
class MyThread
{
public:
struct MyData
{
MyData()
bj(nullptr), done(true){}
Movement* obj;
bool done;
int gg = 10;
};
MyThread()
{
auto th = std::thread(&MyThread::Update, this, std::ref(myData));
tv.push_back(std::move(th));
}
void Update(MyData data)
{
while (1)
{
// Here still uses old values....
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << myData.gg << std::endl;
}
}
bool Replace(Movement* theobj, int newNum)
{
if (myData.done)
{
myData.gg = newNum;
myData.obj = theobj;
myData.done = false;
return true;
}
return false;
}
MyData myData;
};
********************************************
There is a problem where std::ref is not working. When I call the Replace function from the main thread, running the Update function still uses the old default values. Any ideas?
Thanks.
given the following code:
********************************
using ThreadVector = std::vector<std::thread>;
ThreadVector tv;
class MyThread
{
public:
struct MyData
{
MyData()
bj(nullptr), done(true){}Movement* obj;
bool done;
int gg = 10;
};
MyThread()
{
auto th = std::thread(&MyThread::Update, this, std::ref(myData));
tv.push_back(std::move(th));
}
void Update(MyData data)
{
while (1)
{
// Here still uses old values....
std::this_thread::sleep_for(std::chrono::seconds(2));
std::cout << myData.gg << std::endl;
}
}
bool Replace(Movement* theobj, int newNum)
{
if (myData.done)
{
myData.gg = newNum;
myData.obj = theobj;
myData.done = false;
return true;
}
return false;
}
MyData myData;
};
********************************************
There is a problem where std::ref is not working. When I call the Replace function from the main thread, running the Update function still uses the old default values. Any ideas?
Thanks.