开发者

What are the prominent differences in the Boost Thread library or the Pthreads? [duplicate]

This question already has answers here: 开发者_Python百科 Closed 11 years ago.

Possible Duplicate:

PThread vs boost::thread?

I have been using pthreads library programming examples since the time I understood what is multithreading. Of late I came across the Boost thread library on the internet and got curious. Can anyone specify what are the prominent differences between the two? Are there any extra privileges suppiled from Boost, If yes then what are those?


The design of boost::thread was strongly influenced by pthreads, but of course styled as a C++ library instead of a C library. Here are few differences that come to my mind. I do not claim this to be an exhaustive list of differences.

Things pthreads has that boost::thread lacks:

When you use boost::thread you can grab the underlying pthread_t (or pthread_mutex_t, etc.) by calling the native_handle() member function, and use it to regain back functionality not supplied directly by boost::thread.

  • Set scheduling parameters (pthread_attr_setschedparam)
  • Stack query, manipulation (pthread_attr_getstacksize)
  • Mutex/priority query, manipulation (pthread_mutex_getprioceiling)

Things boost::thread has that pthreads lacks:

The following things can be done in pthreads (after all boost::thread is implemented on pthreads). But there isn't clear and direct API in pthreads to do these things.

  • Different types for a thread handle, and a thread id
  • A thread id that can represent "not any thread"
  • The ability to launch a thread on an arbitrary functor with arbitrary arguments
  • The ability to "call once" an arbitrary functor with arbitrary arguments
  • The ability for a condition variable to wait on an arbitrary lockable type
  • The ability to lock multiple mutexes at once without deadlock
  • A portable way to store thread id's in associative containers
  • RAII support for unlocking mutexes


In my experience the boost::thread library has functionality that very closely ties to pthreads. There are some things provided in pthreads that are external to the boost::thread library, but still available in boost (such as semaphores as provided in boost::interprocess).

The main benefits I've seen is the ability to [more] easily write cross-platform code. Since the boost::thread library appears to be implemented as a cross-platform interface layer to various operating system primitives, I don't believe there are significant additional features to be found in boost::thread that wouldn't be available in the actual operating system APIs.

In fact, there are however some underlying features of Windows that I have not been able to find a true equivalent for, mostly from my experience with Win32, and almost certainly because of my limited exposure to what is provided in both the boost::thread and pthread libraries.


From the documentation of Boost.Thread:

Boost.Thread enables the use of multiple threads of execution with shared data in portable C++ code:

Portable

pthread is something that belongs to unix systems, so your code won't be portable to (say) Windows if you use them in your code.

C++

Boost.Thread is a C++ library, while pthread is a "C" library: if your application is written in C++, then you can take advantage of that to simplify your code (e.g. putting Threads objects in containers that understands the Movable concept).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜