C语言 Pthreads 与 OpenMP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3949901/
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
Pthreads vs. OpenMP
提问by Matt Ball
I'm creating a multi-threaded application in C using Linux.
我正在使用 Linux 用 C 创建一个多线程应用程序。
I'm unsure whether I should use the POSIX thread API or the OpenMP API.
我不确定应该使用 POSIX 线程 API 还是 OpenMP API。
What are the pros & cons of using either?
使用两者的优缺点是什么?
Edit:
编辑:
Could someone clarify whether both APIs create kernel-levelor user-levelthreads?
有人可以澄清这两个 API 是创建内核级线程还是用户级线程吗?
回答by Matt Ball
Pthreads and OpenMP represent two totally different multiprocessing paradigms.
Pthreads 和 OpenMP 代表了两种完全不同的多处理范例。
Pthreadsis a very low-level API for working with threads. Thus, you have extremely fine-grained control over thread management (create/join/etc), mutexes, and so on. It's fairly bare-bones.
Pthreads是用于处理线程的非常低级的 API。因此,您可以非常精细地控制线程管理(创建/加入/等)、互斥锁等。这是相当简单的。
On the other hand, OpenMPis muchhigher level, is more portable and doesn't limit you to using C. It's also much more easily scaled than pthreads. One specific example of this is OpenMP's work-sharing constructs, which let you divide work across multiple threads with relative ease. (See also Wikipedia's pros and cons list.)
在另一方面,OpenMP的是多更高水平,更便于携带,并且不限制你使用C.它也更容易比并行线程扩展。这方面的一个具体示例是 OpenMP 的工作共享结构,它使您可以相对轻松地跨多个线程分配工作。(另请参阅维基百科的利弊列表。)
That said, you've really provided no detail about the specific program you're implementing, or how you plan on using it, so it's fairly impossible to recommend one API over the other.
也就是说,您确实没有提供有关您正在实施的特定程序的详细信息,或者您计划如何使用它,因此推荐一种 API 而不是另一种 API 是相当不可能的。
回答by Oliver Charlesworth
If you use OpenMP, it canbe as simple as adding a single pragma, and you'll be 90% of the way to properly multithreaded code with linear speedup. To get the same performance boost with pthreads takes a lot more work.
如果您使用 OpenMP,它可以像添加单个 pragma 一样简单,并且您将 90% 的方式以线性加速正确地多线程代码。要使用 pthreads 获得相同的性能提升,需要做更多的工作。
But as usual, you get more flexibility with pthreads.
但是像往常一样,使用 pthread 可以获得更大的灵活性。
Basically, it depends on what your application is. Do you have a trivially-parallelisable algorithm? Or do you just have lots of arbitrary tasks that you'd like to simultaneously? How much do the tasks need to talk to each other? How much synchronisation is required?
基本上,这取决于您的应用程序是什么。你有一个平凡可并行的算法吗?或者您是否只想同时执行许多任意任务?有多少任务需要相互交谈?需要多少同步?
回答by Reed Copsey
OpenMP has the advantages of being cross platform, and simpler for some operations. It handles threading in a different manner, in that it gives you higher level threading options, such as parallelization of loops, such as:
OpenMP 具有跨平台的优点,并且某些操作更简单。它以不同的方式处理线程,因为它为您提供更高级别的线程选项,例如循环的并行化,例如:
#pragma omp parallel for
for (i = 0; i < 500; i++)
arr[i] = 2 * i;
If this interests you, and if C++ is an option, I'd also recommend Threading Building Blocks.
如果您对此感兴趣,并且可以选择 C++,我还建议您使用Threading Building Blocks。
Pthreads is a lower level API for generating threads and synchronization explicitly. In that respect, it provides more control.
Pthreads 是用于显式生成线程和同步的较低级别的 API。在这方面,它提供了更多的控制。
回答by Zack Yezek
It depends on 2 things- your code base and your place within it. The key questions are- 1) "Does you code base have threads, threadpools, and the control primitives (locks, events, etc.)" and 2) "Are you developing reusable libraries or ordinary apps?"
这取决于两件事——你的代码库和你在其中的位置。关键问题是 - 1)“您的代码库是否具有线程、线程池和控制原语(锁、事件等)”和 2)“您正在开发可重用的库还是普通的应用程序?”
If your library has thread tools (almost always built on some flavor of PThread), USE THOSE. If you are a library developer, spend the time (if possible) to build them. It is worth it- you can put together much more fine-grained, advanced threading than OpenMP will give you.
如果您的库有线程工具(几乎总是建立在某种 PThread 风格上),请使用这些工具。如果您是库开发人员,请花时间(如果可能)构建它们。这是值得的 - 您可以将比 OpenMP 提供的更细粒度、更高级的线程组合在一起。
Conversely, if you are pressed for time or just developing apps or something off of 3rd party tools, use OpenMP. You can wrap it in a few macros and get the basic parallelism you need.
相反,如果您时间紧迫,或者只是开发应用程序或第三方工具之外的东西,请使用 OpenMP。您可以将其包装在几个宏中并获得所需的基本并行性。
In general, OpenMP is good enough for basic multi-threading. Once you start getting to the point that you're managing system resourced directly on building highly async code, its ease-of-use advantage gets crowded out by performance and interface issues.
一般来说,OpenMP 对于基本的多线程来说已经足够好了。一旦你开始管理直接构建高度异步代码的系统资源,它的易用性优势就会被性能和接口问题挤掉。

