正如你可以通过编译下面的代码中看到,并给出了预期的输出,提高::绑定是使用boost ::线程提供免费功能,成员函数和静态成员函数完全没有必要:
#include <boost/thread/thread.hpp>
#include <iostream>
void FreeFunction()
{
std::cout << "hello from free function" << std::endl;
}
struct SomeClass
{
void MemberFunction()
{
std::cout << "hello from member function" << std::endl;
}
static void StaticFunction()
{
std::cout << "hello from static member function" << std::endl;
}
};
int main()
{
SomeClass someClass;
// this free function will be used internally as is
boost::thread t1(&FreeFunction);
t1.join();
// this static member function will be used internally as is
boost::thread t2(&SomeClass::StaticFunction);
t2.join();
// boost::bind will be called on this member function internally
boost::thread t3(&SomeClass::MemberFunction, someClass);
t3.join();
}
输出:
hello from free function
hello from static member function
hello from member function
构造函数中的内部绑定为您完成所有工作。
刚刚添加了几个额外的评论,每个函数类型会发生什么。 (希望我已经正确地阅读了源文件!)据我所知,使用boost :: bind external.The不会导致它加倍并在内部调用,因为它会按原样传递。
所以我认为你真的在问为什么使用Boost来处理(本地)p线程的线程? – ScoPi
@ScoPi:不,我想他是问为什么要使用'bind'而不是让'boost :: thread'构造函数完成所有工作。 (如果你想使用'shared_ptr'来启动线程,我很确定你需要使用'bind'。) –