C++11 thread implementation backend [duplicate] - c++

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
In g++ is C++ 11 thread model using pthreads in the background?
I have read from somewhere that OpenMP is implemented using PThreads in Linux systems although they seem quite different to me. Considering the (relative) similarities between C++11 threads and PThreads I was wondering,
Does anyone know if C++11 threads implemented using PThreads or any other multithreading library in gcc or clang?

There are multiple parts of C++ 2011 multi-threading:
Higher-level abstractions like std::thread, std::mutex, std::condition_variable, etc. These abstractions are implemented in terms of pthreads for both libc++ (clang's native library) and libstdc++ (gcc's native library). libstdc++ uses an indirection (gthr.h) which can be used to, e.g., stub things out for a single threaded implementation. This is quite obvious from the source of the different synchronization classes.
The lower-level synchronization facilities, i.e., atomics and the various memory visibility controls, are not available from pthreads. It seems both gcc and clang implement these using compiler build-ins which probably create suitable instructions. I haven't tracked down the actual code for either of these, however.
It isn't sufficient to implement things in the library: the compiler needs to be prevented from reordering instruction across synchronization primitives and it needs to make values visible in appropriate locations.

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().

C++ & boost::threads - How to prioritize thread based on work type?

I am using C++ with boost on Linux.
What is the best way to prioritize a thread based on work type?
Is it possible to vary a thread priority in POSIX / pthread / Linux?
As far as I know boost doesn't provide an API to do it (the C++11 standard certainly doesn't at least).
On Linux, you can nice or setpriority each thread independently from the others. Note however that this is not POSIX-conformant:
According to POSIX, the nice value is a per-process setting. However, under the current Linux/NPTL implementation of POSIX threads, the nice value is a per-thread attribute: different threads in the same process can have different nice values. Portable applications should avoid relying on the Linux behavior, which may be made standards conformant in the future.
Since Linux uses pthreads (and so does the Linux port of boost) you could also use pthread_setschedparam which has the advantage of being more portable than the Linux-specific per-thread nice behaviour.
In both cases, there's the slight uneasiness due to having to resort to system facilities in order to manage supposedly "opaque" boost (or std in C++11) resources, so tread carefully (as with anything implementation-specific).

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)

Linux threads for dummies. Can some one explain the difference between multi-thread libraries in linux?

New to linux and c++.
I wante to create an application that only needs to run on linux (ubuntuz) and i need to use basic read-write locks.
I saw that there are a few libraries that give "concurrency capabilities".
e.g. to use mutexes, there are at least 3 options:
pthread_mutex_lock (pthread.h)
boost::mutex
std::mutex (mutex)
could someone explain the differences between the various approaches?
pthreads is a C-API and is available on all posix conformant systems (pthreads stands for Posix THREADS).
boost::mutex is a C++-only API that depends on the "boost"-library (you cannot use it in C-code; you add a dependency on "boost")
lots of features from boost will eventually end up in the C++ standard library, e.g. threading; with C++11 you have std::mutex, but you will need a compiler recent enough to support that recent addition. e.g. if you want your application to be backportable to older distributions you might want to avoid it.
std::mutex is part of the standard library. Every C++ compiler/library vendor is free to implement it in any way they like. A library implementation for Posix will more likely than not just use pthreads under the hood, while for instance a Windows library would use the Windows API primitives.
If you can, use std::mutex, but if you don't have C++11 support yet, write your own SBRM classes that wrap the pthread mutex and locking primitives, or use Boost if that's feasible (e.g. if your project already uses Boost anyway). It'll all come down to the same thing anyway.

Is there any cross-platform threading library in C++?

I'm looking for some easy to use cross-platform threading library written in C++.
What's your opinion on boost::thread or Pthreads?
Does Pthreads run only on POSIX compliant systems?
What about the threading support in the Qt library?
Boost.Thread is the draft for the coming standard threading library of the C++ language. Knowing that, I prefer to use it as it provide some strong guarantees (because it becomes standard).
Update: Now that we have the standard threading libraries, some more precisions. Some boost constructs, like boost::shared_mutex, have not been standardised (but might be later). However the standard library exploit the move semantic better. Good to know before choosing a library. Also, using C++11 threading library requires a compiler that provides it. It's not the case for all compilers today.
Update:
Now [Nov2012] most of the Standard compilers provide C++11 threading library. VS2012, GCC4.8 and Clang3.1 have support for threads and synchronization primitives and atomic operations.
For complete implementation you can also use just thread by Anthony Williams. It is C++11 compliant library supported on Windows/Mac and Linux.
Links for status of C++11 features with various compilers:
GCC 4.8 - http://gcc.gnu.org/gcc-4.8/cxx0x_status.html
Clang3.1 - http://clang.llvm.org/cxx_status.html
VS2012 - http://msdn.microsoft.com/en-us/library/vstudio/hh567368.aspx
There is a threading library coming with C++11. It's built upon the boost threading library. Unfortunately, I seem to remember that there are non-trivial differences between Boost.Threads and what C++11 comes with. Still, if you plan to switch to the C++ standard threading library, I believe Boost.Threads is the closest you can get to now.
I suppose that, under the hood, these libraries will use Pthreads on POSIX systems and whatever native threading support is available elsewhere.
Disclaimer: I haven't worked with either of the two.
Pthreads are running only on POSIX systems. QThread from Qt is a way to go. It is available on platforms: Linux, Mac OS X, Windows, Embedded Linux, Windows CE, Symbian, Maemo.
Also have a look at OpenMP, it's a set of (somewhat standard) pragmas specifications that is supported by most major compilers. The good of OpenMP is that it's simple and that your code can be easily compiled in both single and multi-threaded versions.
Just a simple example:
std::vector<double> a, b;
...
double sum = 0.0;
...
#pragma omp parallel for reduction(+:sum)
for (i=0; i < n; i++)
sum = sum + (a[i] * b[i]);
It's obviously possible to do also more complex things.
I am surprised that nobody mentioned the Intel TBB library (linked to an another answer of mine). Also, a task-based implementation should be preferred over a thread-based.
Qt has pretty good thread support. If you just need to create a thread and run some code in it, QThread is all you need. There are many other high-level classes that can help you with thread pools or even abstract the concurrent execution (the QtConcurrent framework).
List the concerning platforms. If you're only using say, Linux/Mac/Windows, then boost::thread will likely do you fine until C++0x (harhar) provides std::thread.
I have used pthreads for code that work on multiple platforms. To get around the Windows lack of pthreads, I have used the following open source library with great success: POSIX Threads for Windows
wxWidgets has thread classes, and as wxWidgets is platform independent, it might just be the best thing for u.
Boost.Threads is built on top of PThreads on UNIX systems and Win32 Threads on Windows.
The boost library is syntactically simple and all of the hairy business of properly interfacing C++ code with C libraries is taken care of behind the scenes. If you're not very comfortable with C++, however, PThreads might seem more straight-forward with its simple C API.
Qt Threads is also a good library, but because I use several other boost libraries, I'll compile and link against Boost no matter what. I might not always link against Qt. And, well, I just don't want to remember how to use two different libraries.
SDL is simple, cross-platform and has threading support.
Pthread is part of Posix, but not every posix systems will have threads. pthreads is most portable.
What platforms will you support?