gpt4 book ai didi

c++ - C++11的多线程模型

转载 作者:太空狗 更新时间:2023-10-29 19:53:43 24 4
gpt4 key购买 nike

我想了解更多关于c++11多线程模型的知识。谷歌搜索我可以找到很多示例、演示等,但有些东西对我来说并不明显。

我们在大学的操作系统类(class)中研究过线程概念。当然,这与特定的实现或语言无关。以下段落来自 Abraham Silberschatz、Greg Gagne 和 Peter Baer Galvin,“操作系统概念,第八版”,第 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 putinto 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

那么c++11支持哪些模型呢? AFAIK c++ 不应该用于特定种类的操作系统。所以标准库应该只支持用户空间线程,对吧?

最佳答案

关于你问的问题(即用户级或内核级线程,以及它们的实现),C++11 标准没有规定没有。因此,任何遵循标准规则的实现(并且可以是您提到的任何一个 - 规则不涉及线程模型的实现细节)都可以。当然,如果你写的是用户空间的程序,自然遵循C++11的线程就是用户空间的线程。但是该标准对“用户空间”或“内核空间”一无所知,因此如果您选择在 C++11 中实现操作系统内核,线程将是(自然如上)内核线程。

在某种意义上,C++11 标准不支持这些线程模型(例如,没有 API 来获取线程并指定“这必须是内核线程,或单独的 lwp 或其他东西”)。另一方面,它支持所有这些(例如,基于任何用户内核线程映射概念构建的线程库可以提供 C++11 线程)。

关于c++ - C++11的多线程模型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11378498/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com