From the boost::thread documentation it seems that I can pass parameters to the thread function by doing this:
boost::thread* myThread = new boost::thread(callba
The problem is that member functions take an implicit first parameter Type*
, where Type
is the type of the class. This is the mechanism by which member functions get called on instances of types, and means you have to pass an extra parameter to the boost::thread
constructor. You also have to pass the address of the member function as &ClassName::functionName
.
I have made a small compiling and running example that I hope illustrates the use:
#include <boost/thread.hpp>
#include <iostream>
struct Foo
{
void foo(int i)
{
std::cout << "foo(" << i << ")\n";
}
void bar()
{
int i = 42;
boost::thread t(&Foo::foo, this, i);
t.join();
}
};
int main()
{
Foo f;
f.bar();
}