Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

Using boost thread and a non-static class function

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?

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

like image 245
deinocheirus Avatar asked Dec 06 '12 11:12

deinocheirus


4 Answers

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:

#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.

like image 188
goji Avatar answered Sep 20 '22 01:09

goji


There is no difference - thread contructor uses bind internally. People use bind explicitly for historical reasons, because Boost.Thread didn't have a "binding" constructor before 1.36.

like image 34
Igor R. Avatar answered Sep 20 '22 01:09

Igor R.


The boost::bind is 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.

like image 32
Tony The Lion Avatar answered Sep 22 '22 01:09

Tony The Lion


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::bind with this and a member function, or a static member function with a void* mapped to this - that's a matter of style mostly).

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

like image 36
utnapistim Avatar answered Sep 23 '22 01:09

utnapistim