1

I have a code base that is using many instances of detached boost::thread as folllows

boost::thread th( boost::bind(&myFunc,var1, var2 ));

Can I simply do a search replace and start using std::thread?

Do I need to replace boost::bind with std::bind too or that is unnecessary?

( I found I could do search replace boost::shared_ptr with std::shared_ptr and boost::scoped_ptr with std::unique_ptr so far without any issues.

My platform is Visual Studio 11 on Windows 7.

user841550
  • 1,067
  • 3
  • 16
  • 25
  • 1
    [You may be interested in this question.](http://stackoverflow.com/questions/7241993/is-it-smart-to-replace-boostthread-and-boostmutex-with-c11-equivalents) – Cornstalks Nov 15 '12 at 16:33

1 Answers1

2

Can I simply do a search replace and start using std::thread?

The current version of boost::thread has a few features that aren't in std::thread:

  • join with timeout
  • thread attributes
  • interruption
  • yield() and sleep() (deprecated)

See the boost documentation, where they are labelled EXTENSION. If you're not using any of those, then it should be a direct replacement.

As mentioned in the comments, older versions of boost::thread had a different behaviour on destruction. They would detach the thread if it was still joinable; std::thread and the current boost::thread call std::terminate instead.

Do I need to replace boost::bind with std::bind too or that is unnecessary?

That's not necessary; thread can be given any callable type. It's also not necessary to use bind at all, since it has a variadic constructor:

std::thread th(&myFunc, var1, var2);
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • Another difference is that `boost::thread`'s destructor just detaches, while `std::thread` requires an explicit call to `detach` or it will assert in the destructor. – Travis Gockel Nov 15 '12 at 16:45
  • 1
    @TravisGockel: That's true for older versions of Boost. The current version behaves like `std::thread` in this respect; see [here](http://www.boost.org/doc/libs/release/doc/html/thread/thread_management.html#thread.thread_management.thread.destructor) – Mike Seymour Nov 15 '12 at 16:46