linux - C++11:Linux 上的 std::thread 是否依赖于 pthread 库?

标签 linux multithreading c++11 pthreads

我读到 pthread 是 C 库并且与 C++ 对象模型不兼容,尤其是在谈论异常处理时。

所以我想知道在linux系统上,gcc/clang是如何实现std::thread的,是调用了一些linux原生函数/kernel api还是什么?

还有,std::thread_local是怎么实现的,跟__thread有关系吗?

最佳答案

I read that pthread is C library and is not compatible with C++ object model, especially when talking about exception handling.

此信息不准确。

how gcc/clang implements std::thread

它们调用特定于平台的线程创建函数。在 Linux 上它是 pthread_create。您可以直接调用此函数。

当线程抛出异常但未被捕获时,调用 std::terminate

请注意,您的应用程序必须使用 -pthread 标志进行编译和链接(使用 -lpthread 是不必要的,并且对于 C 和 C++ 来说是不够的)。

关于linux - C++11:Linux 上的 std::thread 是否依赖于 pthread 库?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45632720/

相关文章:

linux - 在 Cmake 中检测底层平台/ flavor

asp.net - ASP.NET何时会杀死新线程?

Java IO vs NIO vs 任务队列

java - 多线程应用程序中编译时和运行时类路径不匹配的原因是什么?

qt - g++由qmake使用,gcc由套件配置

c++ - 具有通用 vector 和对类型的对 vector ,模板模板

使用 PROMPT_COMMAND 时,Python venv 模块无法将虚拟环境名称添加到 PS1?

linux - enqueue_wakeup宏linux的含义

c - 同一目标模块中的过程调用是否需要在链接阶段重定位

c++ - 用 nullptr 初始化