c++ - Boost Thread 库或 Pthreads 的显着差异是什么?

标签 c++ multithreading boost pthreads

<分区>

Possible Duplicate:
PThread vs boost::thread?

自从我了解什么是多线程以来,我一直在使用 pthreads 库编程示例。最近我在互联网上偶然发现了 Boost 线程库并且很好奇。谁能具体说明两者之间的显着区别是什么? Boost 是否提供了任何额外的特权,如果是,那么这些特权是什么?

最佳答案

boost::thread 的设计受到 pthreads 的强烈影响,但当然风格是 C++ 库而不是 C 库。以下是我想到的一些差异。我并不认为这是一份详尽的差异 list 。

pthreads 具有而 boost::thread 缺乏的东西:

When you use boost::thread you can grab the underlying pthread_t (or pthread_mutex_t, etc.) by calling the native_handle() member function, and use it to regain back functionality not supplied directly by boost::thread.

  • 设置调度参数(pthread_attr_setschedparam)
  • 堆栈查询、操作(pthread_attr_getstacksize)
  • 互斥锁/优先级查询、操作(pthread_mutex_getprioceiling)

boost::thread 具有 pthreads 缺乏的东西:

The following things can be done in pthreads (after all boost::thread is implemented on pthreads). But there isn't clear and direct API in pthreads to do these things.

  • 线程句柄和线程 id 的不同类型
  • 可以表示“不是任何线程”的线程id
  • 在带有任意参数的任意仿函数上启动线程的能力
  • 使用任意参数“调用一次”任意仿函数的能力
  • 条件变量等待任意可锁定类型的能力
  • 一次锁定多个互斥体而不会出现死锁的能力
  • 在关联容器中存储线程 ID 的可移植方式
  • RAII 支持解锁互斥量

关于c++ - Boost Thread 库或 Pthreads 的显着差异是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6928651/

相关文章:

c++ - 为什么在GCC上调用std::package_task失败

c++ - 在 C++ 中使用序列化模拟对象进行单元测试

multithreading - 单线程 Windows 程序的最佳 AWS EC2 实例类型

c# - 使用代码调用任务进行单元测试同步方法

c++ - 为什么 BOOST 创建的这个 JSON 字符串与我的服务器所需的不同?

c++ - 捕获重复组的正则表达式

数组名称的 C++ 值

c++ - 你如何实例化智能指针类成员?

C - 全局指针可以被不同的线程修改吗?

c++ - Boost Asio延迟写入tcp套接字