简体   繁体   English

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

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

I'm looking for some easy to use cross-platform threading library written in C++. 我正在寻找一些易于使用的用C ++编写的跨平台线程库。

What's your opinion on boost::thread or Pthreads ? 你对boost::threadPthreads什么看法? Does Pthreads run only on POSIX compliant systems? Pthreads是否仅在符合POSIX标准的系统上运行?

What about the threading support in the Qt library? Qt库中的线程支持怎么样?

Boost.Thread is the draft for the coming standard threading library of the C++ language. Boost.Thread是即将推出的C ++语言标准线程库的草案。 Knowing that, I prefer to use it as it provide some strong guarantees (because it becomes standard). 知道这一点,我更喜欢使用它,因为它提供了一些强有力的保证(因为它成为标准)。

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). 一些boost构造,如boost :: shared_mutex,尚未标准化(但可能会在以后)。 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. 此外,使用C ++ 11线程库需要一个提供它的编译器。 It's not the case for all compilers today. 今天的所有编译器都不是这样。

Update: Now [Nov2012] most of the Standard compilers provide C++11 threading library. 更新:现在[Nov2012]大多数标准编译器都提供C ++ 11线程库。 VS2012, GCC4.8 and Clang3.1 have support for threads and synchronization primitives and atomic operations. VS2012,GCC4.8和Clang3.1支持线程和同步原语以及原子操作。 For complete implementation you can also use just thread by Anthony Williams. 对于完整的实现,您也可以使用Anthony Williams的线程。 It is C++11 compliant library supported on Windows/Mac and Linux. 它是Windows / Mac和Linux上支持的C ++ 11兼容库。

Links for status of C++11 features with various compilers: 各种编译器的C ++ 11功能的链接:

There is a threading library coming with C++11. C ++ 11附带了一个线程库。 It's built upon the boost threading library. 它建立在boost线程库之上。 Unfortunately, I seem to remember that there are non-trivial differences between Boost.Threads and what C++11 comes with. 不幸的是,我似乎记得Boost.Threads和C ++ 11附带的东西之间存在非平凡的差异。 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 ++标准线程库,我相信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. 免责声明:我没有与两者中的任何一个合作过。

Pthreads are running only on POSIX systems. Pthreads仅在POSIX系统上运行。 QThread from Qt is a way to go. 来自Qt QThread是一种方法。 It is available on platforms: Linux, Mac OS X, Windows, Embedded Linux, Windows CE, Symbian, Maemo. 它可以在平台上使用: Linux,Mac OS X,Windows,嵌入式Linux,Windows CE,Symbian,Maemo。

Also have a look at OpenMP , it's a set of (somewhat standard) pragma s specifications that is supported by most major compilers . 另请参阅OpenMP ,它是大多数主要编译器支持的一组(有些标准) pragma规范。 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的优点在于它很简单,您的代码可以在单线程和多线程版本中轻松编译。

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 more complex things . 显然也可以做 复杂的 事情

I am surprised that nobody mentioned the Intel TBB library (linked to an another answer of mine). 我很惊讶没有人提到英特尔TBB库 (链接到我的另一个答案)。 Also, a task-based implementation should be preferred over a thread-based. 此外,基于任务的实现应优先于基于线程的实现。

Qt has pretty good thread support. Qt有很好的线程支持。 If you just need to create a thread and run some code in it, QThread is all you need. 如果您只需要创建一个线程并在其中运行一些代码,那么QThread就是您所需要的。 There are many other high-level classes that can help you with thread pools or even abstract the concurrent execution (the QtConcurrent framework). 还有许多其他高级类可以帮助您使用线程池,甚至可以抽象并发执行(QtConcurrent框架)。

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. 如果您只使用say,Linux / Mac / Windows,那么boost :: thread可能会很好,直到C ++ 0x(harhar)提供std :: thread。

I have used pthreads for code that work on multiple platforms. 我已经将pthread用于在多个平台上运行的代码。 To get around the Windows lack of pthreads, I have used the following open source library with great success: POSIX Threads for Windows 为了解决Windows缺乏pthread问题,我使用了以下开源库并取得了巨大成功: 适用于Windows的POSIX线程

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

Boost.Threads is built on top of PThreads on UNIX systems and Win32 Threads on Windows. Boost.Threads建立在UNIX系统上的PThreads和Windows上的Win32 Threads之上。

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. boost库在语法上很简单,所有在C ++代码与C库正确连接的毛茸茸的业务都在幕后处理。 If you're not very comfortable with C++, however, PThreads might seem more straight-forward with its simple C API. 但是,如果你对C ++不太满意,那么通过简单的C API,PThreads似乎更直接。

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. Qt Threads也是一个很好的库,但是因为我使用了其他几个boost库,所以无论如何我都会编译并链接到Boost。 I might not always link against Qt. 我可能并不总是与Qt联系。 And, well, I just don't want to remember how to use two different libraries. 而且,我只是不想记住如何使用两个不同的库。

SDL简单,跨平台并具有线程支持。

Pthread is part of Posix, but not every posix systems will have threads. Pthread是Posix的一部分,但不是每个posix系统都有线程。 pthreads is most portable. pthreads是最便携的。

What platforms will you support? 你会支持哪些平台?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM