C++ 使用 boost::bind() 或不使用 boost::bind() 创建一个 boost::thread

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13742648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:41:04  来源:igfitidea点击:

Creating a boost::thread with boost::bind() or without

c++boostboost-thread

提问by deinocheirus

Some people seem to launch boost::threads using the boost::bind() function, like in the accepted answer of the following question:

有些人似乎使用 boost::bind() 函数启动 boost::threads,就像在以下问题的公认答案中一样:

Using boost thread and a non-static class function

使用 boost 线程和非静态类函数

Whereas other people don't use it at all, like in the answer with the most upvotes of this question:

而其他人根本不使用它,就像在这个问题中获得最多投票的答案中一样:

Best way to start a thread as a member of a C++ class?

作为 C++ 类的成员启动线程的最佳方法?

So, what's the difference, if it exists?

那么,如果存在,又有什么区别呢?

回答by goji

As you can see by the code below that compile and gives the expected output, boost::bind is completely unnecessary for using boost::thread with free functions, member functions and static member functions:

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

#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();
}

Output:

输出:

hello from free function
hello from static member function
hello from member function

The internal bind in the constructor does all the work for you.

构造函数中的内部绑定为您完成所有工作。

Just added a few extra comments on what happens with each function type. (Hopefully I've read the source correctly!) As far as I can see, using boost::bind externally will not cause it to also double up and be called internally as it will pass through as is.

只是添加了一些关于每种函数类型会发生什么的额外评论。(希望我已经正确阅读了源代码!)据我所知,在外部使用 boost::bind 不会导致它也加倍并在内部调用,因为它会按原样通过。

回答by Igor R.

There is no difference - threadcontructor uses bindinternally. People use bindexplicitly for historical reasons, because Boost.Thread didn't have a "binding" constructor before 1.36.

没有区别 -thread构造函数在bind内部使用。人们bind出于历史原因明确使用,因为 Boost.Thread在 1.36 之前没有“绑定”构造函数

回答by Tony The Lion

The boost::bindis used to bind a member function to a thread, whereas without boost::bind normally you're using a static function or a free function with the thread.

boost::bind用于一个成员函数绑定到一个线程,而无需升压::绑定正常您使用的是静态函数或线程免费功能。

回答by utnapistim

So, what's the difference, if it exists?

那么,如果存在,又有什么区别呢?

The main difference is what do you need to access within the thread function.

主要区别在于您需要在线程函数中访问什么。

If your design requires that you access a class instance's data, then launch your thread as part of a class instance (use boost::bindwith thisand a member function, or a static member function with a void*mapped to this- that's a matter of style mostly).

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

If your design requires that the thread function is not dependent on a particular object's data, then use a free function.

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

回答by tpg2114

The primary difference is whether you want to interface static or non-static member functions. If you want to use non-static member functions as the function launched by the thread, you must use something like bind.

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

The proposed alternative (the second question you linked) is to use a static method that takes a pointer to the class object and can then call any of it's members. This clears up the syntax slightly but the biggest advantage (to me) is that you don't need to include something like Boostto get bind. But if you are using boost::threadsyou might as well take boost::bindalso. Note, C++ 11 has std::bindso you could use bindwith pthreadsas well and not introduce any extra dependency such as Boost, but that's if you want to use C++ 11.

建议的替代方案(您链接的第二个问题)是使用静态方法,该方法采用指向类对象的指针,然后可以调用它的任何成员。这稍微清理了语法,但最大的优势(对我来说)是你不需要包含像Boostget 之类的东西bind。但如果你正在使用,boost::threads你也可以采取boost::bind。请注意,C++ 11 具有,std::bind因此您也可以使用bindwithpthreads并且不会引入任何额外的依赖项,例如 Boost,但前提是您想使用 C++ 11。

I don't see a compelling syntax reason to avoid using bindover having a static method that calls member functions. But that is more a matter of personal preference.

我没有看到一个令人信服的语法理由来避免使用bind调用成员函数的静态方法。但这更多是个人喜好问题。