Multithreading: apache portable runtime vs boost::thread? - c++

Which way is better for a novice student who has never used boost?
When a new standard arrives, the answer will be obvious, but now I have doubts. Pro for boost is that it's much closer to future standard.

Another pro for Boost is that it uses common C++ idioms for describing, initializing, running, and joining threads (using idioms like RAII and allowing the use of functors and similar C++-specific tools).
Lower-level libraries like APR rely on the use of C-like idioms, which for a C++ developer can prove to be more error-prone (such as the use of function pointers).
In my opinion, Boost::Thread is easier to use because it allows me to use the C++ idioms that I use elsewhere in my code. With APR, I may as well be using pthreads.

As boost::thread can be seen as the draft for the comming standard library implementation of threads, using it and maybe replace it by std::thread in some years might be the best thread library to know first.

Related

For what purpose may synchronization primitives and containers from Boost library be needed?

For what purpose may synchronization primitives and containers from Boost library be needed if the project uses C++ 11/14/17 in which there are already containers and synchronization primitives?
I know that Boost.asio is usually used in work with the network, Boost.spirit - usually for parsing of text. Do you know about the usual purpose of the other parts of Boost?
This question is from the C++ interview.
Boost is older than C++ 11 so many synchronization priminitives were there before they made it to the standard. This was feasible because the OSes already contained thread and synchronization functions so boost could wrap around them.
That said, at this point the C++ standard allows for trivial threading/synchronization. It's adequate for the average C++ developer. In complex sync scenarios you might need some boost-enhanced libraries, or even OS-dependant APIs, for example in Windows, WaitForMultipleObjects().

How does the standard threading library compare to Boost’s?

I am re-writing a bit of legacy code in C++ 11 and wanted to make the processing more concurrent. I have read about C++11 threading library but wanted to see if anyone has used this and if you'd able to share you thoughts on how easy it is to implement?
Between Boost threading and this library, which one is more preferable and why?
Boost threading library is the same than the standard one (if you activate the new interface) but it adds features which are currently proposed as extension for C++14/17. It also propose more synchronization tools than the current standard or even C++14 draft.
What you need to know is that the standard and boost libraries are actually low-level concurrent constructs, they don't provide yet higher-level constructs, but there is work by both the standard commitee and Boost developers to add such constructs.
I recommand reading the book "C++ concurrency in action" which describe in depths how the C++11 threading library work and what it don't provide too (like thread pools implementations).

C++ 11 Thread vs Boost Thread is there any difference? [duplicate]

This question already has answers here:
Is it smart to replace boost::thread and boost::mutex with c++11 equivalents?
(7 answers)
Closed 9 years ago.
What are the advantages/disadvantages of using the C++11 multithreading classes versus the ones found in Boost? I will only be using Linux so I do not require portability. Is there a lack of features in one of the libraries? Any known limitations? Better syntax?
Standard threads have the advantage of being standardised, therefore portable to any compliant implementation.
The Boost thread library is more or less identical; the standard library was based on that library, and there has been an effort to make Boost a conformant implementation of the standard. It has a few extensions which might be useful, including:
join with timeout
thread interruption
thread groups
extra lock types
In general, boost classes are only wrappers around functions/objects that exist in given OS. Their main advantage is that boost contains versions written for most operating systems, hence the wrapper provides portability the original functions/objects sometimes do not.
If there is nothing else your need from boost I would strongly suggest using standard C++11 threads.
Reasons:
boost will not provide more than the system allows for
your code will not have any wrapper overhead (however small it may be)
boost support for c++11 threads is a new feature and I would fear that it could introduce some errors in the boosts' implementation
you will not have to rely on boost libraries and will save yourself time compiling and linking them, etc.
you will not have to update boost, because you will not be using it
Of course, boost has some pros also:
many people know boost and the code will (possibly) be easier to read
if you decide you need to port the code you may have an easier time (though C++11 is standard, so somewhere down the line all compilers will implement it)

Is there a common design pattern or object oriented approach used to encapsulate thread behaviour?

I have recently been using pthreads on Linux and want to start looking into using boost threads in the near future. I have never used MS visual studio so I don't know the approach there but I (have to) use Embarcadero C++ Builder (formerly Borland) one of the few good things I find with it is that is has a built in class TThread. This is a class that you can derive from to give nicely encapsulated data and start and terminate functions. I prefer this approach than the pthread way of passing functions and void* being passed into the thread create function. I was wondering if there is some kind of design pattern or structure that is commonly used to provide a more object oriented that encapsulates the functionality of threads in this way? I can try and create it myself, but I cannot be the first person to desire this kind of approach and wondered if there was a "standard" way of achieving it.
Edit: Alternatively, if this is a very bad idea perhaps an illustration of why?
I would consider that the most standard approach would be using the standard thread library (closely related to boost::thread, not 100% the same). I would avoid redesigning what has already been designed, reviewed and verified by a committee of experts.
Also note that for the kind of operations that you mention in the comment you might want to take a look at the future part of the standard library (again similar to the boost counterpart), and in particular to the std::asynch function.
I like the way boost::thread is designed (I saw a similar design in the threading library from Rouge Wave). Basically a thread is started by passing a function to be executed and latter that thread object can be used to join that thread. pthread is very similar in design, but boost::thread is much easier to use.

PThread vs boost::thread?

Having no experience with threading in the past, which threading technique in C++ will be the easiest for a beginner? boost::thread or pthreads?
I'll go in the opposite direction of everyone else - learn (or at least familiarize yourself with what is available in) pthreads.
Since boost is mainly just a wrapper around pthreads (on posix platforms) it helps to know what is going on underneath. In attempting to be generic, boost leaves the platform specific functionality unwrapped. In order to get to it you need to use the native_handle() calls. In order to use the native_handle() calls you need to know what the platform offers.
Think of it like sockets. There are dozens of socket classes and frameworks. But ultimately they wrap the underlying platform's socket API. Your understanding is always richer by knowing it - and knowing in what ways your class abstractions might have short comings.
Go for boost::thread. It's closely related to the work on the upcoming C++ standard threads, and the interface is quite easy to use and idiomatic to C++ (RAII instead of manual resource management).
boost::thread is a very nice and portable abstraction. I would certainly use it, but also learn the native thread api, like pthreads, so that you know how threading works on your platform.
Boost.Thread uses the RAII concept for locking, which makes things more exception safe and helps to avoid bugs like forgetting to release a mutex.
I'd say they're pretty close to equal in difficulty. The only big difference I see is that PThreads are pretty widely support (if you're concerned with cross platform porting). Another is that there have been quite a few good books on PThreads, though almost all the concepts will translate over to boost::thread, and many other threading libraries.