c++ multithread help.

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() :obj(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.
 

davidktw

Arch-Supremacy Member
Joined
Apr 15, 2010
Messages
13,547
Reaction score
1,301
Code:
using ThreadVector = std::vector<std::thread>;
ThreadVector tv;

class MyThread
{
public:

	struct MyData
	{
		MyData() :obj(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([COLOR="Yellow"][B]MyData data[/B][/COLOR]) //should it be [COLOR="Lime"][B]MyData &data[/B][/COLOR]
	{
		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;
};

Do everyone a favour, next time, wrap your codes within the CODE tags.

Look at the codes in YELLOW

Run the following codes and you will understand better what went wrong
Code:
#include <iostream>

using namespace std;

class Complex {
public:
  int i;

  Complex() :i(123) {}
};

void func(Complex c) {
}

int main() {

  Complex c;

  cout << "Variable 'c' at memory location: " << &c;
  cout << ", Value of i = " << c.i << endl;

  Complex d  = ref(c);
  Complex &e = ref(c);

  cout << "Variable 'd' at memory location: " << &d;
  cout << ", Value of i = " << d.i << endl;
  cout << "Variable 'e' at memory location: " << &e;
  cout << ", Value of i = " << e.i << endl;

  d.i = 321;
  cout << "After variable 'd', member 'i' value is changed" << endl;
  e.i = 456;
  cout << "After variable 'e', member 'i' value is changed" << endl;

  cout << "Variable 'c' at memory location: " << &c;
  cout << ", Value of i = " << c.i << endl;
  cout << "Variable 'd' at memory location: " << &d;
  cout << ", Value of i = " << d.i << endl;
  cout << "Variable 'e' at memory location: " << &e;
  cout << ", Value of i = " << e.i << endl;

}

There is an implicit COPY operation that happened.
 
Last edited:

invisible.hippo

Arch-Supremacy Member
Joined
Nov 8, 2008
Messages
17,902
Reaction score
315
Code:
using ThreadVector = std::vector<std::thread>;
ThreadVector tv;

class MyThread
{
public:

	struct MyData
	{
		MyData() :obj(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([COLOR="Yellow"][B]MyData data[/B][/COLOR]) //should it be [COLOR="Lime"][B]MyData &data[/B][/COLOR]
	{
		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;
};

Do everyone a favour, next time, wrap your codes within the CODE tags.

Look at the codes in YELLOW

Run the following codes and you will understand better what went wrong
Code:
#include <iostream>

using namespace std;

class Complex {
public:
  int i;

  Complex() :i(123) {}
};

void func(Complex c) {
}

int main() {

  Complex c;

  cout << "Variable 'c' at memory location: " << &c;
  cout << ", Value of i = " << c.i << endl;

  Complex d  = ref(c);
  Complex &e = ref(c);

  cout << "Variable 'd' at memory location: " << &d;
  cout << ", Value of i = " << d.i << endl;
  cout << "Variable 'e' at memory location: " << &e;
  cout << ", Value of i = " << e.i << endl;

  d.i = 321;
  cout << "After variable 'd', member 'i' value is changed" << endl;
  e.i = 456;
  cout << "After variable 'e', member 'i' value is changed" << endl;

  cout << "Variable 'c' at memory location: " << &c;
  cout << ", Value of i = " << c.i << endl;
  cout << "Variable 'd' at memory location: " << &d;
  cout << ", Value of i = " << d.i << endl;
  cout << "Variable 'e' at memory location: " << &e;
  cout << ", Value of i = " << e.i << endl;

}

There is an implicit COPY operation that happened.

MyData() : obj(nullptr)
Is this inheritance?
 
Important Forum Advisory Note
This forum is moderated by volunteer moderators who will react only to members' feedback on posts. Moderators are not employees or representatives of HWZ Forums. Forum members and moderators are responsible for their own posts. Please refer to our Community Guidelines and Standards and Terms and Conditions for more information.
Top