C++11 std::threads 与 posix 线程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13134186/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 17:00:45  来源:igfitidea点击:

C++11 std::threads vs posix threads

c++multithreadingc++11pthreads

提问by Shamdor

Why should I prefer one or another in practice? What are technical differences except that std::threadis a class?

为什么我应该在实践中更喜欢一个或另一个?除了std::thread是一个类之外,还有什么技术差异?

采纳答案by Gunther Piez

If you want to run code on many platforms, go for Posix Threads. They are available almost everywhere and are quite mature. On the other hand if you only use Linux/gcc std::threadis perfectly fine - it has a higher abstraction level, a really good interface and plays nicely with other C++11 classes.

如果您想在多个平台上运行代码,请选择 Posix Threads。它们几乎随处可用,并且非常成熟。另一方面,如果您只使用 Linux/gcc,std::thread则完全没问题——它具有更高的抽象级别,一个非常好的界面,并且可以很好地与其他 C++11 类配合使用。

The C++11 std::threadclass unfortunately doesn't work reliably (yet) on every platform, even if C++11 seems available. For instance in native Android std::threador Win64 it just does not work or has severe performance bottlenecks (as of 2012).

std::thread不幸的是,即使 C++11 似乎可用,C++11类也不能在每个平台上可靠地工作(还)。例如,在原生 Androidstd::thread或 Win64 中,它无法正常工作或存在严重的性能瓶颈(截至 2012 年)。

A good replacement is boost::thread- it is very similar to std::thread(actually it is from the same author) and works reliably, but, of course, it introduces another dependency from a third party library.

一个很好的替代品是boost::thread- 它与std::thread(实际上来自同一作者)非常相似并且工作可靠,但是,当然,它引入了来自第三方库的另一个依赖项。



Edit: As of 2017, std::threadmostly works on native Android. Some classes, like std::timed_mutexare still not implemented.

编辑:截至 2017 年,std::thread主要适用于原生 Android。一些类,比如std::timed_mutex仍然没有实现。

回答by Akira Takahashi

The std::threadlibrary is implemented on top of pthreads in an environment supporting pthreads (for example: libstdc++).

std::thread库是在支持 pthreads 的环境中的 pthreads 之上实现的(例如:libstdc++)。

I think the big difference between the two is abstraction. std::threadis a C++ class library. The std::threadlibrary includes many abstract features, for example: scoped locks, recursive mutexes, future/promise design pattern implementations, and more.

我认为两者之间的最大区别在于抽象。std::thread是一个 C++ 类库。该std::thread库包括许多抽象功能,例如:作用域锁、递归互斥锁、未来/承诺设计模式实现等。

回答by Brady

std::threadprovides portability across different platforms like Windows, MacOS, and Linux.

std::thread提供跨不同平台(如 Windows、MacOS 和 Linux)的可移植性。

As mentioned by @hirshhornsalz in the comments below and related answer https://stackoverflow.com/a/13135425/1158895, std::threadmay not be complete on all platforms yet. Even still, (it will be in the near future) it should be favored over pthread's because it should make your application more future-proof.

正如@hirshhornsalz 在下面的评论和相关答案https://stackoverflow.com/a/13135425/1158895 中所提到的,std::thread可能尚未在所有平台上完成。即便如此,(它将在不久的将来)它应该比pthread's更受青睐,因为它应该使您的应用程序更加面向未来。

回答by Waslap

For me the deciding technical difference is the absence of signal handling primitives in std as opposed to pthreads. The inability to properly dictate signal handling in a Unix process using std alone is AFAIK a debilitating flaw in the use of std::thread as it bars one from setting up the bona fide multi-threaded signal handling pattern to process all signals in a dedicated thread and block them in the rest. You are forced to assume std::thread is implemented using pthreads and hope for the best when using pthread_sigmask. Handling signals properly is non-negotiable in Unix systems programming for the enterprise.

对我来说,决定性的技术差异是 std 中没有信号处理原语,而不是 pthread。无法在 Unix 进程中单独使用 std 正确指示信号处理是 AFAIK 使用 std::thread 的一个令人衰弱的缺陷,因为它阻止一个人设置真正的多线程信号处理模式来处理所有信号。线程并在其余部分阻止它们。您被迫假设 std::thread 是使用 pthreads 实现的,并希望在使用 pthread_sigmask 时获得最佳效果。在企业的 Unix 系统编程中,正确处理信号是不容商量的。

As at 2016, std::thread is a toy; simple as that.

截至 2016 年,std::thread 是一个玩具;就那么简单。

回答by Martin Vahi

The OpenMP

OpenMP

http://www.openmp.org/

http://www.openmp.org/

is a standardized, SMP based multithreading standard that has been working on Linux and Windows for over a decade already. The OpenMP is available by default with all compilers, including GCC and Microsoft Visual Studio.

是一个标准化的、基于 SMP 的多线程标准,已经在 Linux 和 Windows 上工作了十多年。默认情况下,OpenMP 可用于所有编译器,包括 GCC 和 Microsoft Visual Studio。

One thing to watch out for, when using the OpenMP, is that if there are more threads than there are CPU-cores, then the performance will go down due to the context switching related overhead. The second thing to keep in mind is that the initialization of an actual, operating system level, thread is relatively expensive. The initialization is a fraction of a second, but in some applications the very small fractions accumulate to a considerable expense.

在使用 OpenMP 时需要注意的一件事是,如果线程数多于 CPU 内核数,那么由于上下文切换相关的开销,性能将会下降。要记住的第二件事是,实际的操作系统级别线程的初始化相对昂贵。初始化是几分之一秒,但在某些应用程序中,非常小的部分累积起来会产生相当大的费用。

For software architecture requirements related concurrency You may want to search for some implementation of "lightweight threads" or "green threads" in stead of using the OpenMP. The difference is that the OpenMP threads are actual, operating system level, threads, but the "green threads" can be just "simulated threads" that are executed by using some small number of real threads.

对于与并发性相关的软件架构要求,您可能想要搜索“轻量级线程”或“绿色线程”的一些实现,而不是使用 OpenMP。不同之处在于 OpenMP 线程是实际的、操作系统级别的线程,但“绿色线程”可以只是使用少量真实线程执行的“模拟线程”。