Soroush Rabiei
Soroush Rabiei

Reputation: 10868

Multithreading model of C++11

I want to know more about c++11 multithreading model. Googling around I can find a lot of examples, demonstrations etc. but something is not obvious to me.

We've studied about threading conceps in OS lessons of university. Of course that was not about a specific implementation or language. following passage is from Abraham Silberschatz, Greg Gagne, and Peter Baer Galvin, "Operating System Concepts, Eighth Edition ", Chapter 4:

4.2 Multithreading Models

There are two types of threads to be managed in a modern system: User threads and kernel threads. User threads are supported above the kernel, without kernel support. These are the threads that application programmers would put into their programs.

Kernel threads are supported within the kernel of the OS itself. All modern OSes support kernel level threads, allowing the kernel to

  • perform multiple simultaneous tasks and/or to service multiple kernel
  • system calls simultaneously.

In a specific implementation, the user threads must be mapped to kernel threads, using one of the following strategies:

4.2.1 Many-To-One Model

4.2.2 One-To-One Model

4.2.3 Many-To-Many Model

So which of models is supported by c++11? AFAIK c++ is not supposed to be used in a specific variety of OSs. So standard library should support only user space threads, right?

Upvotes: 3

Views: 1383

Answers (1)

jpalecek
jpalecek

Reputation: 47762

Regarding the issue you ask about (that is, user-level or kernel-level threads, and their implementation), the C++11 standard specifies nothing. Therefore, any implementation that follows the rules of the standard (and that could be any of the ones you mention - the rules do not concern the implementation details of the threading model) goes. Of course, if you write a user-space program, it naturally follows C++11 threads will be user-space threads. But the standard doesn't know anything about "userspace" or "kernelspace", so if you choose to implement an OS kernel in C++11, the threads will be (naturally as above) kernel threads.

In some sense, the C++11 standard supports none of those threading models (eg. there is no API to get a thread and specify "this must be a kernel thread, or a separate lwp or something"). In another, it supports all of them (eg. threading libraries built on any of the user-kernel thread mapping concepts can provide C++11 threads).

Upvotes: 10

Related Questions