什么类型的多线程最适合学习?
我想学习 C++ 中的多线程,但我不确定哪种类型最有用。我看过的教程是:
- Windows 本机调用
- OpenMP
- Boost
(我确信可能还有更多。)
每个教程的主要功能是什么以及它们最适合做什么?
注意:我已经通过手动创建线程在 C# 中完成了一些多线程处理,更复杂的线程只会让它变得更有趣。 :)
I want to learn multi-threading in C++ but I'm not sure what type would be most useful. The ones I've seen tutorials on are:
- Windows native calls
- OpenMP
- Boost
(I'm sure that there are probably more.)
What is each one's key features and what are they best used for?
Note: I've already done some multi-threading in C# by manually creating the threads, and more complexity of the threading will just make it more fun. :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您有更多 C 背景,我会从 pthreads 开始;如果您习惯更惯用的 C++,我会从 Boost Thread 开始。两者都相当便携且广泛使用。
I'd start with pthreads if you have more of a C background, or Boost Thread if you are accustomed to more idiomatic C++. Either is reasonably portable and widely used.
TBB怎么样?它是可移植的,并且具有易于使用的并行模板模式、并发容器、任务调度程序和可扩展的内存分配器。 TBB 可以让您直接管理线程,但在大多数情况下这不是必需的。
就我个人而言,我会远离特定于平台的线程,除非迫切需要做一些特定于平台的事情。
Boost 线程可移植且易于使用,但既没有并行模式,也没有并发容器。您需要手动管理线程,这很快就会变得丑陋。
PThreads 在 Windows 及其 C 上不可用。您确实想在 C++ 中进行多线程,而不是 C。RAII 与互斥体和作用域锁很好地混合。
另一个选项是 Visual C++ 2010 中的 PPL。它与 TBB 类似,但您可能会猜到仅适用于 Windows。
OpenMP 易于使用,但不太灵活。既然你已经学过 C++,你应该使用更严肃的东西,比如 TBB 或 PPL。由于某些奇怪的原因,Visual C++ 2010 不支持 OpenMP 3。太糟糕了。
How about TBB? It is portable and has easy to use parallel template patterns, concurrent containers, task scheduler and scalable memory allocaturs. TBB will let you manage threads directly, but that is not necessary in most of the cases.
Personally I would stay away from platform specific threads, unless there an urgent need to do something, well, platform specific.
Boost threads is portable and easy to use, but does have neither parallel patterns nor concurrent containers. You would need to manager threads manually, which can get ugly pretty quickly.
PThreads isn't available on Windows and its C. You really want to do multi-threading in C++, not C. RAII mixes well with mutexes and scoped locks.
Another option is PPL in Visual C++ 2010. It is similar to TBB, but as you may guess available for Windows only.
OpenMP is easy to use, but not very flexible. Since you already learned C++, you should use something more serious, such as TBB or PPL. For some strange reason Visual C++ 2010 doesn't support OpenMP 3. Too bad.
如果您想要可移植,请学习 Posix 线程。你知道,所有线程库都提供或多或少相同的一组功能,所以这取决于你,但 Posix 会给你基础。
openMP 并不完全是您所说的“多线程”。
If you want to be portable, learn Posix threads. You know, all thread libraries provide more or less the same set of features, so it's up to you, but Posix will give you the basis.
openMP isn't exactly "multi-threading" as you mean it.
WinThreads (Windows) 和 pthreads (Linux) 是 POSIX 线程,可能是您入门的最佳选择。了解进程和线程之间的区别,然后了解与它们相关的各种内存访问模型非常重要。接下来,尝试 OpenMP 和 MPI“线程”等并发方法。
有一些基本概念会被重复。好好学习它们。
WinThreads (Windows) and pthreads (Linux) are POSIX threads and represent probably your best choice to get started. It is important to learn the distinction between processes and threads, then learn about the various memory access models that are associated with them. Next, try concurrency approaches like OpenMP and MPI "threads".
There are some basic concepts that will get repeated. Learn them well.