I’m using an object to start boost thread and it has some public member variables which I modify in the thread (in the () operator). How can I access the object’s member variables from outside the thread?
I tried using a mutex (defined in the object’s class) that is locked both in the operator() of the object and from outside, but it doesn’t seem to work.
Here’s the thread object code:
struct Mouse { int x, y; string port; boost::mutex mutex; Mouse(const string& p) : port(p) { x = y = 0; } Mouse(const Mouse& m) : mutex() { x = m.x; y = m.y; port = m.port; } void operator()() { ifstream ifs; ifs.open (port.c_str(), ios::binary ); if (!ifs.is_open()) { cout << 'Impossible d'ouvrir ' << port.c_str() << '\n'; exit(0); } while (true) //modify x, y in infinit loop { char buf[3]; ifs.read(buf, 3); unsigned char * msg = (unsigned char *) buf; unsigned char xsign = (msg[0]>>4) & 1; unsigned char ysign = (msg[0]>>5) & 1; unsigned char always1 = (msg[0]>>3) & 1; short dx = msg[1] - 256*xsign; short dy = msg[2] - 256*ysign; { boost::mutex::scoped_lock lock(mutex); x += abs(dx); y += dy; } } } };
And this is where I try to access the x and y variables of the mouse:
{ boost::mutex::scoped_lock leftlock(leftMouse.mutex); xLeft = leftMouse.x; yLeft = leftMouse.y; } { boost::mutex::scoped_lock rightlock(rightMouse.mutex); xRight = rightMouse.x; yRight = rightMouse.y; } cout << xRight << ' ' << yRight << endl; //this always prints 0 0
boost::threadcopies the passed thread function to internal storage, so if you start your thread like this, the thread will operate on a different copy ofmouse:You can use
boost::refto pass a reference to an existing object instead:In this case
thrwill modify the globalmouseobject, but you have to make sure thatmousedoesn’t go out of scope or gets destroyed otherwise beforethris finished.