Reputation: 12336
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(callbackFunc, param);
However, when I do this the compiler complains that
no overloaded function takes 2 arguments
My code:
#include <boost/thread/thread.hpp>
void Game::playSound(sf::Sound* s) {
boost::thread soundThread(playSoundTask, s);
soundThread.join();
}
void Game::playSoundTask(sf::Sound* s) {
// do things
}
I am using the copy of boost that came with Ogre3d, which I suppose could be very old. Interestingly, though, I took a look at thread.hpp and it does have the templates for constructors with 2 or more parameters.
Upvotes: 2
Views: 2902
Reputation: 227578
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();
}
Upvotes: 6