C++ 中是否有任何跨平台线程库?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2561471/
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 23:55:07  来源:igfitidea点击:

Is there any cross-platform threading library in C++?

c++multithreadingc++11cross-platform

提问by NumberFour

I'm looking for some easy to use cross-platform threading library written in C++.

我正在寻找一些用 C++ 编写的易于使用的跨平台线程库。

What's your opinion on boost::threador Pthreads? Does Pthreads run only on POSIX compliant systems?

您对boost::thread或 有Pthreads什么看法?Pthreads 是否只在符合 POSIX 的系统上运行?

What about the threading support in the Qtlibrary?

Qt库中的线程支持怎么样?

回答by Klaim

Boost.Thread is the draft for the coming standard threading library of the C++ language. Knowing that, I prefer to use it as it provide some strong guarantees (because it becomes standard).

Boost.Thread 是即将到来的 C++ 语言标准线程库的草案。知道这一点,我更喜欢使用它,因为它提供了一些强有力的保证(因为它成为标准)。

Update: Now that we have the standard threading libraries, some more precisions. Some boost constructs, like boost::shared_mutex, have not been standardised (but might be later). However the standard library exploit the move semantic better. Good to know before choosing a library. Also, using C++11 threading library requires a compiler that provides it. It's not the case for all compilers today.

更新:现在我们有了标准的线程库,精度更高了。一些 boost 构造,比如 boost::shared_mutex,还没有标准化(但可能会更晚)。然而,标准库更好地利用了移动语义。很高兴在选择图书馆之前了解。此外,使用 C++11 线程库需要一个提供它的编译器。今天并非所有编译器都如此。

Update: Now [Nov2012] most of the Standard compilers provide C++11 threading library. VS2012, GCC4.8 and Clang3.1 have support for threads and synchronization primitives and atomic operations. For complete implementation you can also use just thread by Anthony Williams. It is C++11 compliant library supported on Windows/Mac and Linux.

更新:现在 [2012 年 11 月] 大多数标准编译器都提供 C++11 线程库。VS2012、GCC4.8 和 Clang3.1 支持线程和同步原语和原子操作。对于完整的实现,您还可以仅使用 Anthony Williams 的线程。它是 Windows/Mac 和 Linux 上支持的 C++11 兼容库。

Links for status of C++11 features with various compilers:

各种编译器的 C++11 特性状态链接:

回答by sbi

There is a threading library coming with C++11. It's built upon the boost threading library. Unfortunately, I seem to remember that there are non-trivial differences between Boost.Threads and what C++11 comes with. Still, if you plan to switch to the C++ standard threading library, I believe Boost.Threads is the closest you can get to now.

C++11 附带了一个线程库。它建立在 boost 线程库之上。不幸的是,我似乎记得 Boost.Threads 和 C++11 附带的东西之间存在着不小的差异。尽管如此,如果您打算切换到 C++ 标准线程库,我相信 Boost.Threads 是您现在最接近的。

I suppose that, under the hood, these libraries will use Pthreads on POSIX systems and whatever native threading support is available elsewhere.

我想,在幕后,这些库将在 POSIX 系统上使用 Pthreads,并且在其他地方可以使用任何本机线程支持。

Disclaimer: I haven't worked with either of the two.

免责声明:我没有与两者中的任何一个合作过。

回答by baol

Also have a look at OpenMP, it's a set of (somewhat standard) pragmas specifications that is supported by most major compilers. The good of OpenMP is that it's simple and that your code can be easily compiled in both single and multi-threaded versions.

也看看OpenMP,它pragma大多数主要编译器支持的一组(有些标准)的规范。OpenMP 的优点在于它很简单,并且您的代码可以轻松地编译为单线程和多线程版本。

Just a simple example:

只是一个简单的例子:

std::vector<double> a, b;
...
double sum = 0.0;
...
#pragma omp parallel for reduction(+:sum)
  for (i=0; i < n; i++)
    sum = sum + (a[i] * b[i]);

It's obviously possible to do also morecomplexthings.

显然也可以做复杂的事情

回答by zoli2k

Pthreadsare running only on POSIX systems. QThreadfrom Qtis a way to go. It is available on platforms: Linux, Mac OS X, Windows, Embedded Linux, Windows CE, Symbian, Maemo.

Pthreads仅在 POSIX 系统上运行。QThreadfromQt是一条路。它适用于以下平台:Linux、Mac OS X、Windows、嵌入式 Linux、Windows CE、Symbian、Maemo。

回答by amit

I am surprised that nobody mentioned the Intel TBB library(linked to an another answer of mine). Also, a task-based implementation should be preferredover a thread-based.

我很惊讶没有人提到英特尔 TBB 库(链接到我的另一个答案)。此外,基于任务的实现应该优于基于线程的实现。

回答by Starkey

I have used pthreads for code that work on multiple platforms. To get around the Windows lack of pthreads, I have used the following open source library with great success: POSIX Threads for Windows

我已经将 pthreads 用于在多个平台上工作的代码。为了解决 Windows 缺少 pthreads 的问题,我使用了以下开源库并取得了巨大成功: POSIX Threads for Windows

回答by Puppy

List the concerning platforms. If you're only using say, Linux/Mac/Windows, then boost::thread will likely do you fine until C++0x (harhar) provides std::thread.

列出相关平台。如果您只使用 Linux/Mac/Windows,那么 boost::thread 可能会很好,直到 C++0x (harhar) 提供 std::thread。

回答by Luká? Lalinsky

Qt has pretty good thread support. If you just need to create a thread and run some code in it, QThreadis all you need. There are many other high-level classes that can help you with thread pools or even abstract the concurrent execution (the QtConcurrent framework).

Qt 有很好的线程支持。如果您只需要创建一个线程并在其中运行一些代码,这QThread就是您所需要的。还有许多其他高级类可以帮助您处理线程池甚至抽象并发执行(QtConcurrent 框架)。

回答by the100rabh

wxWidgetshas thread classes, and as wxWidgets is platform independent, it might just be the best thing for u.

wxWidgets有线程类,因为 wxWidgets 是平台无关的,它可能对你来说是最好的。

回答by Dr. Watson

Boost.Threads is built on top of PThreads on UNIX systems and Win32 Threads on Windows.

Boost.Threads 建立在 UNIX 系统上的 PThreads 和 Windows 上的 Win32 线程之上。

The boost library is syntactically simple and all of the hairy business of properly interfacing C++ code with C libraries is taken care of behind the scenes. If you're not very comfortable with C++, however, PThreads might seem more straight-forward with its simple C API.

boost 库在语法上很简单,所有将 C++ 代码与 C 库正确连接的繁琐事务都在幕后处理。但是,如果您对 C++ 不是很满意,那么 PThreads 可能看起来更简单,因为它具有简单的 C API。

Qt Threads is also a good library, but because I use several other boost libraries, I'll compile and link against Boost no matter what. I might not always link against Qt. And, well, I just don't want to remember how to use two different libraries.

Qt Threads 也是一个很好的库,但是因为我使用了其他几个 boost 库,所以无论如何我都会针对 Boost 进行编译和链接。我可能并不总是链接到 Qt。而且,好吧,我只是不想记住如何使用两个不同的库。