Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::threads are managed in user or kernel space?

As I wrote in the title, I would like to know if c++ stantard threads are managed in user or kernel space.

Thank you.

like image 469
Aslan986 Avatar asked Apr 30 '12 16:04

Aslan986


People also ask

Can kernel level threads be managed by the user?

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.

Is STD thread user-level thread?

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.

How do you implement threads in user space?

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.

What does std :: thread do?

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


2 Answers

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.

like image 196
Matteo Italia Avatar answered Oct 16 '22 12:10

Matteo Italia


The interface is designed around pthreads, but it is up to the implementer of the libc++ to decide what to use.

like image 23
Ignacio Vazquez-Abrams Avatar answered Oct 16 '22 14:10

Ignacio Vazquez-Abrams