As I wrote in the title, I would like to know if c++ stantard threads are managed in user or kernel space.
Thank you.
Kernel-level threads are handled by the operating system directly and the thread management is done by the kernel. The context information for the process as well as the process threads is all managed by the kernel. Because of this, kernel-level threads are slower than user-level threads.
You can be assured that std::thread is not using "user threads" because that concept pretty much died around the turn of the century. Modern hardware has multiple CPU cores, which work much better if there are sufficient kernel threads. Without enough kernel threads, CPU cores may sit idle.
Step 1 − The complete thread package is placed in the user space and the kernel has no knowledge about it. Step 2 − Kernel generally, manages ordinary and single threaded processes. Step 3 − Threads are always run on top of a run-time system. Step 4 − Run time system is a collection of procedures which manage threads.
std::thread Threads allow multiple functions to execute concurrently. std::thread objects may also be in the state that does not represent any thread (after default construction, move from, detach, or join), and a thread of execution may not be associated with any thread objects (after detach).
As happens almost always, the standard doesn't mandate any particular implementation, it just requires that the exhibited behavior conforms to its rules.
Thus, the particular implementation is free to choose; on the other hand, probably many implementations will be based on boost.thread (on which the std::thread
proposal is based), so we can look at it to have an idea.
This library uses pthreads on POSIX and Windows threads on Win32. Win32 threads are definitely kernel threads, but pthreads on their own are just yet another interface, which could be implemented both in user space and in kernel space (although almost any recent UNIX kernel provides facilities to implement them in kernel space).
So: std::thread
can be anything, although, on "mainstream" PC operating systems/implementations, it's very likely that you'll get kernel threads. If for some reason you need to know more, check your compiler's documentation.
The interface is designed around pthreads, but it is up to the implementer of the libc++ to decide what to use.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With