将参数传递给boost::thread无重载函数接受2个参数
Passing parameter to boost::thread no overloaded function takes 2 arguments
从boost::thread文档看来,我可以通过这样做将参数传递给线程函数:
boost::thread* myThread = new boost::thread(callbackFunc, param);
但是,当我这样做时,编译器报错
我代码:没有重载函数接受两个参数
#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
}
我正在使用Ogre3d附带的boost副本,我想它可能很老了。然而,有趣的是,我看了看thread.hpp,它确实有带有2个或更多参数的构造函数模板。
问题是成员函数接受隐式的第一个参数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();
}
相关文章:
- static_assert在宏中,但也可以扩展到可以用作函数参数的东西
- C++中的高效循环缓冲区,它将被传递给C样式数组函数参数
- 当从函数参数中的临时值调用复制构造函数时
- 如何从"decltype()"获取函数参数的数量<funtion>?
- 如何将lambda作为模板类的成员函数参数
- 模板参数推导失败,函数参数/参数不匹配
- 如何在C++中将迭代器作为函数参数传递
- 将函数参数"const char*"转换为"std::string_view"是
- C++ 如何将数组值解压缩为函数参数
- 主函数参数的属性
- 具有两个间接寻址运算符 (C++) 的函数参数的用途
- "Warning: Comma within array index expression"但逗号分隔函数参数
- 如何定义在用作函数参数时工作的类模板的转换
- 将函数参数完美转发到函数指针:按值传递呢?
- 为什么我不能将引用作为 std::async 的函数参数传递
- 什么..(省略号)作为函数原型中唯一的函数参数,C++?
- 是否可以就地构造一个固定大小的数组作为函数参数?
- 接受模板作为函数参数
- 将成员函数作为构造函数参数调用时出错 "Variable is not a type name"
- Arduino 函数参数