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

28,067

Solution 1

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.

Solution 2

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.

Solution 3

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.

Solution 4

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.

Share:
28,067
deinocheirus
Author by

deinocheirus

Updated on January 30, 2020

Comments

  • deinocheirus
    deinocheirus over 4 years

    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?

  • deinocheirus
    deinocheirus over 11 years
    So why do people use it? This boost::asio example uses bind() too: boost.org/doc/libs/1_52_0/doc/html/boost_asio/example/chat/…
  • juanchopanza
    juanchopanza over 11 years
    @user1613 it could depend on the version of boost. The variant without std::bind requires some kind of variadic template emulation.
  • goji
    goji over 11 years
    It has been supported at least as far back as 2008 occording to Anthony Williams Dr Dobbs article on Boost.Thrads: drdobbs.com/cpp/whats-new-in-boost-threads/211600441 I would suspect people do it purely out of habit. Without looking at the source, this probably means boost::bind is called twice if you use it externally.
  • David Schwartz
    David Schwartz over 11 years
    What about using a shared_ptr to call a member function? Does it ensure a copy of the shared pointer stays in scope through the execution of the member function?
  • mark
    mark over 11 years
    People probably use boost::bind because they are unaware of boost::thread's internal bind
  • goji
    goji over 11 years
    @DavidSchwartz of course, boost::bind will create a functor internally storing the shared_ptr for the lifetime of the thread. It's identical to if you did it yourself, only it's done on your behalf.
  • deinocheirus
    deinocheirus over 11 years
    @mark I'm asking mainly because it looks unlikely to me that even boost::asio's author is unaware of that... I think I'll update my question.
  • mark
    mark over 11 years
    @user1613 I have to admit I was unaware of it until today - but then again I haven't had a lot of exposure to boost::thread (recently I have been using std::thread.
  • David Schwartz
    David Schwartz over 11 years
    @Troy: I agree. It appears there's no need to do the bind under any conditions I can think of.