Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 131041
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T06:02:35+00:00 2026-05-11T06:02:35+00:00

I’m using an object to start boost thread and it has some public member

  • 0

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 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. 2026-05-11T06:02:35+00:00Added an answer on May 11, 2026 at 6:02 am

    boost::thread copies the passed thread function to internal storage, so if you start your thread like this, the thread will operate on a different copy of mouse:

    int main() {   Mouse mouse('abc.txt');   boost::thread thr(mouse); // thr gets a copy of mouse   ...   // thread changes it's own copy of mouse   ... } 

    You can use boost::ref to pass a reference to an existing object instead:

      Mouse mouse('abc.txt');   boost::thread thr(boost::ref(mouse)); // thr gets a reference of mouse 

    In this case thr will modify the global mouse object, but you have to make sure that mouse doesn’t go out of scope or gets destroyed otherwise before thr is finished.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm new to using the Perl treebuilder module for HTML parsing and can't figure
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I have a jquery bug and I've been looking for hours now, I can't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.