Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 365
Shraddha Avatar asked Aug 03 '11 15:08

Shraddha


2 Answers

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
like image 159
Howard Hinnant Avatar answered Nov 17 '22 12:11

Howard Hinnant


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.

like image 31
Chad Avatar answered Nov 17 '22 12:11

Chad