Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/158.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 使用boost::bind()或不使用boost::bind()创建boost::线程_C++_Boost_Boost Thread - Fatal编程技术网

C++ 使用boost::bind()或不使用boost::bind()创建boost::线程

C++ 使用boost::bind()或不使用boost::bind()创建boost::线程,c++,boost,boost-thread,C++,Boost,Boost Thread,有些人似乎使用boost::bind()函数启动boost::threads,如以下问题的公认答案所示: 而其他人根本不使用它,比如在这个问题的答案中,得票最多的是: 那么,如果它存在的话,有什么区别呢?boost::bind用于将成员函数绑定到线程,而没有boost::bind的情况下,通常情况下,线程使用的是静态函数或自由函数。主要区别在于您是要连接静态成员函数还是非静态成员函数。如果希望使用非静态成员函数作为线程启动的函数,则必须使用类似于bind的功能 建议的替代方案(您链接的第二个

有些人似乎使用boost::bind()函数启动boost::threads,如以下问题的公认答案所示:

而其他人根本不使用它,比如在这个问题的答案中,得票最多的是:


那么,如果它存在的话,有什么区别呢?

boost::bind用于将成员函数绑定到线程,而没有boost::bind的情况下,通常情况下,线程使用的是静态函数或自由函数。

主要区别在于您是要连接静态成员函数还是非静态成员函数。如果希望使用非静态成员函数作为线程启动的函数,则必须使用类似于
bind
的功能

建议的替代方案(您链接的第二个问题)是使用静态方法,该方法获取指向类对象的指针,然后可以调用它的任何成员。这稍微理清了语法,但是(对我来说)最大的好处是,您不需要包含类似于
Boost
的内容来获得
bind
。但是如果您使用的是
boost::threads
,那么您也可以使用
boost::bind
。注意,C++ 11有代码> STD::绑定< /COD> >因此,可以使用<代码> BIN < /> >使用<代码> pStult<代码>,并且不引入任何额外的依赖关系,如Boost,但如果您想使用C++ 11,则使用。p>
我不认为有什么令人信服的语法理由可以避免使用
bind
而不是调用成员函数的静态方法。但这更多的是个人偏好。

正如您可以从下面编译并给出预期输出的代码中看到的,boost::bind对于将boost::thread与自由函数、成员函数和静态成员函数一起使用是完全不必要的:

#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();
}
构造函数中的内部绑定为您完成所有工作

只是在每个函数类型上添加了一些额外的注释。(希望我正确地阅读了源代码!)据我所知,在外部使用boost::bind不会导致它也加倍并在内部被调用,因为它将按原样通过

那么,如果存在的话,有什么区别呢

主要的区别是在线程函数中需要访问什么

如果您的设计要求访问类实例的数据,那么将线程作为类实例的一部分启动(使用
boost::bind
this
以及成员函数,或者使用映射到
this
void*
静态成员函数,这主要是风格问题)


如果您的设计要求线程函数不依赖于特定对象的数据,则使用自由函数。

没有区别-
thread
constructor在内部使用
bind

人们明确地使用
bind
是出于历史原因,因为Boost.Thread没有一个.

所以我想你是在问为什么对线程使用Boost而不是(本地)p-threads?@ScoPi:不,我想他是在问为什么要使用
bind
而不是让
Boost::Thread
构造函数来完成所有的工作。(我很确定,如果您想使用
共享\u ptr
启动线程,您需要使用
bind
),那么人们为什么要使用它呢?这个boost::asio示例也使用bind()@user1613它可能取决于boost的版本。没有
std::bind
的变体需要某种可变模板模拟。根据Anthony Williams Dr Dobbs在Boost上的文章,它至少早在2008年就得到了支持。Thrads:我怀疑人们这样做纯粹是出于习惯。如果不查看源代码,这可能意味着boost::bind在外部使用时会被调用两次。使用
共享的\u ptr
调用成员函数怎么样?它是否确保共享指针的副本在执行成员函数期间保持在作用域内?人们可能使用
boost::bind
,因为他们不知道
boost::thread
的内部绑定
hello from free function
hello from static member function
hello from member function