将参数传递给boost :: thread没有重载函数需要2个参数

Raz*_*orm 2 c++ boost visual-c++

从boost :: thread文档看来,我可以通过执行以下操作将参数传递给线程函数:

boost::thread* myThread = new boost::thread(callbackFunc, param);
Run Code Online (Sandbox Code Playgroud)

但是,当我这样做时,编译器会抱怨

没有重载函数需要2个参数

我的代码:

#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
}
Run Code Online (Sandbox Code Playgroud)

我正在使用Ogre3d附带的boost副本,我想这可能很老了.有趣的是,我看了一下thread.hpp,它确实有两个或更多参数的构造函数的模板.

jua*_*nza 6

问题是成员函数采用隐式的第一个参数Type*,其中Type是类的类型.这是在类型实例上调用成员函数的机制,这意味着您必须将额外的参数传递给boost::thread构造函数.您还必须将成员函数的地址作为&ClassName::functionName.

我已经做了一个小编译和运行的例子,我希望说明这个用法:

#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();
}
Run Code Online (Sandbox Code Playgroud)