boost::thread ?

Have member function of a class I need to run in a new thread and an argument to it's parameter:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class SomeClass
{
private:
boost::shared_ptr<boost::thread> m_thread;
private:
void SomeFunc(int param)
{
//...
}
public:
void doSomething(int n)
{
  m_thread = boost::shared_ptr< boost::thread > 
    ( new boost::thread(&SomeClass::SomeFunc,n) );

}
};



Obviously the problem is in the doSomething(int n) function. I really am stumped about how to do that.
closed account (3hM2Nwbp)
I'm pretty sure you can use boost::bind for that
1
2
// #include <boost/bind.hpp>
m_thread = boost::shared_ptr<boost::thread>(boost::thread(boost::bind(&SomeClass::SomeFunc, this, n)));


*Edit - didn't notice you were using shared pointers
Last edited on
Wonderful, I'll give that a try.

:D

Oh, but can I ask why I'm binding "this" ?
Last edited on
closed account (3hM2Nwbp)
Basically, you refer to the member function &SomeClass::SomeFunc...but which instance of that class? We pass bind a pointer to the instance of the class that the member function belongs to.

It might be worth looking into std::mem_fun as well - it might be a bit more lightweight than boost::bind
Last edited on
Excellent, thank you!
Topic archived. No new replies allowed.