boost::thread

This is a discussion on boost::thread within the C++ Programming forums, part of the General Programming Boards category; Hello I have a class someclass with somefunction; Now I want to create a boost thread with somefunction: Code: someclass ...

  1. #1
    l2u
    l2u is offline
    Registered User
    Join Date
    May 2006
    Posts
    630

    boost::thread

    Hello

    I have a class someclass with somefunction;

    Now I want to create a boost thread with somefunction:

    Code:
    someclass sc;
    boost::thread thr(&sc.somefunction);
    thr.join();
    What am I doing wrong?
    Should I use bind instead?

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    To start with you aren't doing what you think. You aren't passing a reference to the somefunction() member function.

    You can't get the address of a member function for reasons having to do with the fact member functions add an extra parameter (the *This parameter) . You can however define a static function and create your member function to call it. It is then possible to get the address of a static function with the following syntax:

    &Class::static_function
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    what Mario F. said is true, however you can achieve the effect you're looking for with boost.bind
    Code:
    someclass sc;
    
    // assuming somefunction takes no params
    boost::thread thr(boost::bind(&someclass::somefunction, sc);
    thr.join();
    I love bind. It's just so awesome, I don't think I could program in C++ without it anymore.
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,581
    I'm actually wanting to study boost::bind and the acompanying function object libraries. Only read good things about them. You just shown me another reason
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    semi-colon generator ChaosEngine's Avatar
    Join Date
    Sep 2005
    Location
    Chch, NZ
    Posts
    597
    it is cool, but you have to be careful. when you find yourself writing code like this
    Code:
    std::for_each (m_elementList.begin(),
                   m_elementList.end(), 
                   bind(TiXmlElement::SetAttribute,
                        pNewElement,
                        bind(XMLBaseElement::GetName, _1),
                        bind(XMLBaseElement::GetStringValue, _1)));
    // equiv to 
    // for each iter in m_elementList
    //      pNewElement->SetAttribute((*iter)->GetName(), (*iter)->GetStringValue());
    it's probably time to just use a for loop
    "I saw a sign that said 'Drink Canada Dry', so I started"
    -- Brendan Behan

    Free Compiler: Visual C++ 2005 Express
    If you program in C++, you need Boost. You should also know how to use the Standard Library (STL). Want to make games? After reading this, I don't like WxWidgets anymore. Want to add some scripting to your App?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inconsistent (and wrong) output using boost::thread
    By Fox5 in forum C++ Programming
    Replies: 2
    Last Post: 12-29-2008, 01:42 AM
  2. To multithread or not ?
    By serge in forum C++ Programming
    Replies: 6
    Last Post: 06-16-2008, 01:15 AM
  3. Terminate Boost::Thread
    By CornyKorn21 in forum C++ Programming
    Replies: 3
    Last Post: 06-03-2008, 01:19 PM
  4. Know of any good Boost::Thread tutorials
    By indigo0086 in forum Tech Board
    Replies: 1
    Last Post: 06-28-2007, 12:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21