linux - 为什么使用 std::thread::hardware_concurrency() 和 boost::thread::hardware_concurrency() 有区别?

标签 linux multithreading boost c++11

问题本身的描述很简单。我正在测试 C++11 中 std::thread 库和 boost::thread 库的区别。

这些的输出:

#include <iostream>
#include <thread>
#include <boost/thread.hpp>

int main() {
  std::cout << std::thread::hardware_concurrency() << std::endl;
  std::cout << boost::thread::hardware_concurrency() << std::endl;
  return 0;
}

给我不同的结果:

0
4

这是为什么?

PS:gcc包的版本是4.6.2-1.fc16(x86_64)。我正在使用

g++ test.cc -Wall -std=c++0x -lboost_thread-mt -lpthread

最佳答案

查看/usr/include/c++/4.6.2/thread后

可以看出实现其实是:

// Returns a value that hints at the number of hardware thread contexts.
static unsigned int
hardware_concurrency()
{ return 0; }

这样问题就解决了。这只是 gcc 4.6.2 中尚未实现的另一个功能

关于linux - 为什么使用 std::thread::hardware_concurrency() 和 boost::thread::hardware_concurrency() 有区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8540387/

相关文章:

linux - 从存档中提取确切文件的快速方法

java - @Async 和 @Transactional 的 Spring 启动问题

c++ - 如何将 Boost 库添加到 RPI2 上的 QTCreator?

java - 如何保证 Java 中文件的原子移动或异常?

java - 取消 future 与停止线程

python - boost.mpi.world Communicator 与 C++ 签名不匹配:boost::mpi::communicator

c++ - 将ZeroMQ与Boost::ASIO一起使用

c++ - Linux 到 Windows C++ 端口

c - Linux C 读取一个目录

linux - 如何以表格格式获取此输出