LoginSignup
10
4

More than 5 years have passed since last update.

C++ で CPU のコア数を調べる方法

Posted at

本文書は、マルチコア対応 Forth 系言語 Paraphrase の開発作業からのスピンオフ企画です(C++ 教材としての Paraphrase / Paraphrase 解体新書プロジェクト)。

まさかのプラットフォーム非依存

 依存ではなく、依存しない方法です。本文書のタイトルでは CPU のコア数とかいちゃっていますが、正確には CPU スレッド数のことです(4 コア 8 CPU スレッドなら 8)。

C++11 の標準機能で取得可能

 C++11 であれば、特にプラットフォームに依存した書き方をしなくても、thread ヘッダを読み込んで、std::thread::hardware_concurrency() にて取得可能です(https://cpprefjp.github.io/reference/thread/thread/hardware_concurrency.html)。Parahrase のコードではこんな感じに書いております(って、そのままですね…):

#include <thread>
unsigned int G_NumOfCores=std::thread::hardware_concurrency();

余談

 しばらく C++ でコードを書いていなかったら、どんどん便利になっており、非常に助かります。便利な機能を標準として取り込んでくれてありがとう。感謝・感謝なのです。

10
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
4