我读到 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/