1

Currently, I am reading the book that contains the usage of both STD library and boost library. The problem is that Visual Studio 2010 doesn't support STD thread yet and we have to depend on the boost library.

Thus I am looking for a one to one mapping between the std thread library and boost library. Is there some resource that I use for reference?

For example,

std::mutex
std::thread
std::lock_guard
std::unique_ptr
std::move
std::thread::hardware_concurrency()
std::thread::id
std::this_thread
std::shared_ptr         => boost::shared_ptr
Oleg Svechkarenko
  • 2,508
  • 25
  • 30
q0987
  • 34,938
  • 69
  • 242
  • 387
  • I would have a look at Intel Threading Building Blocks, it provides a very high level interface to threading and alike, and it plays very nice with C++11 lambdas aswell. – smerlin Dec 22 '11 at 17:10
  • 1
    @smerlin : The problem is that TBB is very unlike `std::thread`, but `boost::thread` is mostly similar. I think the OP wants to know how to directly apply his knowledge of `std::thread` as closely as possible to an alternative library. – ildjarn Dec 22 '11 at 19:23
  • @ildjarn: yeah, i have noticed that, i just wanted to mention TBB since pretty much always it is better to try to use a more high level approach to solve a problem first. If that does not work out because the approach is to high level, you still can start digging in the low level area. And TBB is very easy to use, open source and free for academic purposes. – smerlin Dec 22 '11 at 23:48

3 Answers3

2

You can use std::move, std::unique_ptr, std::shared_ptr in MSVC 2010.

Do you know that you can use VC++11 Developer Preview for free? And there you can use STL synchronization objects. Have a look at webinars of Bartocz Milewski about concurrency

You can see comparison of boost::mutex, boost::thread vs. std::mutex, std::thread in following question Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?

Community
  • 1
  • 1
Oleg Svechkarenko
  • 2,508
  • 25
  • 30
  • The project only allows me to use VS 2010. – q0987 Dec 22 '11 at 15:37
  • AFAIK Bartoscz Milewski uses the `just::thread` library for a implemenation of `std::thread` and related classes (http://www.stdthread.co.uk/). – smerlin Dec 22 '11 at 17:13
  • @smerlin: Yes, hi does. However, I use plain VC++11 with Milewski's webinars and it still works fine. There is the [difference](http://blogs.msdn.com/b/vcblog/archive/2011/09/12/10209291.aspx) between VC++11 and C++11. – Oleg Svechkarenko Dec 23 '11 at 09:10
2

The Boost thread library is very similar to the standard thread library (which was based on the boost library); I think everything from that is more or less the same in Boost, including mutex, thread, lock_guard, thread::hardware_concurrency() and thread::id from your list.

The remaining things aren't part of the thread library; perhaps your compiler does have them, even if it doesn't have that library.

There are implementations of boost::move, but I'm not sure whether they behave like std::move - you should use that if your compiler supports it.

I don't think there is a boost::unique_ptr. You could perhaps emulate it with boost::scoped_ptr, using std::swap (or perhaps boost::move) to emulate a move.

As you say, boost::shared_ptr is very similar to std::shared_ptr.

Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
  • This is exactly my concern. when two names are same in both libraries, it doesn't mean they perform the same. – q0987 Dec 22 '11 at 15:39
  • Boost feature that entered the std namespace are not full equivalent: boost was written for C++03, based on hat experience C++11 was defined, and the new STL is written for C++11. C++03 and C++11 offer different semantic mechanism, hance std::x is strongest than the boost::x that originated it. – Emilio Garavaglia Dec 22 '11 at 15:39
2

You can give a look to boost::thread and relate classes (just look at boost.org).

But don't forget that they are not exact equivalent: boost::thread had been implemented in C++03, while VS2010 is providing a C++11 compiler. The two languages are different in term of "features" and libraries can benefit from C++11 feature more than C++03.

That makes std::shared_ptr (and std::unique_ptr) preferrable to boost::shared_ptr and std::auto:_ptr where C++11 features are available.

The reason MS is not supporting std::thread and the like (but the same is for the windows version of GCC - MinGW) is that std::thread wraps the old C POSIX pthread functionality, but windows -internally- is not a POSIX equivalent system and doesn't support certain primitives (although it offers other).

MS started from Win6 (Vista) to provide POSIX functional equivalent API, thus making the mapping work possible and effective. But that makes std::thread to be available only fro win6+ in a world where win5+ (XP/2 & 3) are still predominant.

Right now, boost::thread is a C++03 mimic of what became std::thread in C++11, and is available (although not perfectly equivalent in term of functional granularity) for both POSIX (Unix/Linux) and Windows.

Emilio Garavaglia
  • 20,229
  • 2
  • 46
  • 63
  • I highly doubt that `std::thread` cant be implemented on windows-xp. Which features of `std::thread` cant be implemented efficiently without pthreads api ? – smerlin Dec 22 '11 at 17:09
  • @smerlin: It actually IS. The problem with windows, is that it has no full-equivalent native object for condition_variable. The windows event (see CreateEvent) can mimic it, but it cannot translate both -in a same instance- notify_one and notify_all. Forgettin pthread, the C++ standard interface can be implemeted in Windows, but requires a more complex (and dedicated) effort than required on Posix system. That makes windows programmers more likely prefer C++ wrapper mapping the Win32 native objects, rather than mimicking the Posix ones. But that leads to programs with a different logic. – Emilio Garavaglia Dec 23 '11 at 07:56
  • Yes, i see your point, but i still dont believe you need that functionality for implementing std::thread`. Of course they cant implement `std::condition_variable` that efficient, but it should be possible to implement most other multithreading features of C++11 without using condition variables. – smerlin Dec 23 '11 at 16:27