C++ PThread 与 boost::thread?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2170222/
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
PThread vs boost::thread?
提问by Chris
Having no experience with threading in the past, which threading technique in C++ will be the easiest for a beginner? boost::thread or pthreads?
在过去没有线程经验的情况下,C++ 中的哪种线程技术对初学者来说最容易?boost::thread 还是 pthreads?
回答by Duck
I'll go in the opposite direction of everyone else - learn (or at least familiarize yourself with what is available in) pthreads.
我将朝着与其他人相反的方向前进 - 学习(或至少让自己熟悉)pthreads。
Since boost is mainly just a wrapper around pthreads (on posix platforms) it helps to know what is going on underneath. In attempting to be generic, boost leaves the platform specific functionality unwrapped. In order to get to it you need to use the native_handle() calls. In order to use the native_handle() calls you need to know what the platform offers.
由于 boost 主要只是围绕 pthreads(在 posix 平台上)的包装器,因此有助于了解下面发生的事情。在尝试通用时,boost 未封装平台特定的功能。为了获得它,您需要使用 native_handle() 调用。为了使用 native_handle() 调用,您需要知道平台提供了什么。
Think of it like sockets. There are dozens of socket classes and frameworks. But ultimately they wrap the underlying platform's socket API. Your understanding is always richer by knowing it - and knowing in what ways your class abstractions might have short comings.
把它想象成套接字。有几十个套接字类和框架。但最终它们包装了底层平台的套接字 API。通过了解它,您的理解总是更加丰富 - 并了解您的类抽象可能有哪些不足之处。
回答by Malte Clasen
Go for boost::thread. It's closely relatedto the work on the upcoming C++ standard threads, and the interface is quite easy to use and idiomatic to C++ (RAII instead of manual resource management).
去提升::线程。它与即将到来的C++ 标准线程的工作密切相关,并且该接口非常易于使用且符合 C++ 的习惯(RAII 而不是手动资源管理)。
回答by Stefan Arentz
boost::thread is a very nice and portable abstraction. I would certainly use it, but also learn the native thread api, like pthreads, so that you know how threading works on your platform.
boost::thread 是一个非常好的和可移植的抽象。我当然会使用它,但也会学习本机线程 api,例如 pthreads,以便您了解线程在您的平台上是如何工作的。
回答by Emile Cormier
Boost.Thread uses the RAII concept for locking, which makes things more exception safe and helps to avoid bugs like forgetting to release a mutex.
Boost.Thread 使用 RAII 概念进行锁定,这使得异常安全并有助于避免诸如忘记释放互斥锁之类的错误。
回答by Chris S
I'd say they're pretty close to equal in difficulty. The only big difference I see is that PThreads are pretty widely support (if you're concerned with cross platform porting). Another is that there have been quite a few good books on PThreads, though almost all the concepts will translate over to boost::thread, and many other threading libraries.
我会说他们在难度上非常接近。我看到的唯一大区别是 PThreads 得到了相当广泛的支持(如果您关心跨平台移植)。另一个是关于 PThreads 的好书很多,尽管几乎所有的概念都会转化为 boost::thread 和许多其他线程库。