本文介绍了C ++ 11线程实现后端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我从某处了解到,使用 PThreads $实现了
OpenMP
在Linux系统中,尽管它们对我来说似乎完全不同。考虑到 C ++ 11
线程和 PThreads
之间的(相对)相似之处,我想知道,
I have read from somewhere that OpenMP
is implemented using PThreads
in Linux systems although they seem quite different to me. Considering the (relative) similarities between C++11
threads and PThreads
I was wondering,
有人知道是否使用 PThreads
实现了 C ++ 11
线程或 gcc
或 clang
中的任何其他多线程库?
Does anyone know if C++11
threads implemented using PThreads
or any other multithreading library in gcc
or clang
?
推荐答案
C ++ 2011多线程有多个部分:
There are multiple parts of C++ 2011 multi-threading:
- 高级-像
std :: thread
,std :: mutex
,std :: condition_variable
等。这些抽象是针对libc ++(c族的本机库)和libstdc ++(gcc的本机库)的pthreads实现的。 libstdc ++使用间接寻址(gthr.h
),可用于例如为单线程实现添加内容。从不同的同步类的来源来看,这是很明显的。 - pthreads不提供较低级别的同步工具,即原子和各种内存可见性控件。看来gcc和clang都使用编译器内置程序来实现这些功能,它们可能会创建合适的指令。但是,我还没有找到这两个代码的实际代码。
- 在库中实现功能还不够:需要防止编译器在同步期间对指令重新排序原语,它需要使值在适当的位置可见。
- Higher-level abstractions like
std::thread
,std::mutex
,std::condition_variable
, etc. These abstractions are implemented in terms of pthreads for both libc++ (clang's native library) and libstdc++ (gcc's native library). libstdc++ uses an indirection (gthr.h
) which can be used to, e.g., stub things out for a single threaded implementation. This is quite obvious from the source of the different synchronization classes. - The lower-level synchronization facilities, i.e., atomics and the various memory visibility controls, are not available from pthreads. It seems both gcc and clang implement these using compiler build-ins which probably create suitable instructions. I haven't tracked down the actual code for either of these, however.
- It isn't sufficient to implement things in the library: the compiler needs to be prevented from reordering instruction across synchronization primitives and it needs to make values visible in appropriate locations.
这篇关于C ++ 11线程实现后端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!