Which standard C++ features can be used for querying machine/OS architecture?

后端 未结 4 1803
难免孤独
难免孤独 2021-02-05 12:07

What are the standard C++ features and utilities for querying the properties of the hardware or operating system capabilities, on which the program is running?
For instance,

相关标签:
4条回答
  • 2021-02-05 12:28

    As others have pointed out, functions for obtaining such properties of the system are typically platform-specific. The STL and boost do not provide platform-independent wrappers, so you'll have to rely on other third party libraries.

    I've successfully used SIGAR in the past:

    The Sigar API provides a portable interface for gathering system information such as:

    • System memory, swap, cpu, load average, uptime, logins
    • Per-process memory, cpu, credential info, state, arguments, environment, open files
    • File system detection and metrics
    • Network interface detection, configuration info and metrics
    • TCP and UDP connection tables
    • Network route table

    As a side note, Boost Filesystem does actually provide boost::filesystem::space to query "how much disk space is available to write to in a certain directory".

    0 讨论(0)
  • 2021-02-05 12:30

    "std::thread::hardware_concurrency() gives you the number of threads the machine supports ..."

    No, it doesn't. To be precise (citing from here)

    std::thread::hardware_concurrency() ... Returns number of concurrent threads supported by the implementation. The value should be considered only a hint. ...
    ... If the value is not well defined or not computable, returns ​0​.

    Best this does is letting you know, how many CPU cores are available for real parallel execution of threads (see @Lightness Races in Orbit's answer here).
    You still can have as many thread instances you want, until acquiring one fails.


    "how do you detect how much RAM the computer has, or how much RAM the process is using, ..."

    All of these capabilities like RAM available etc. are highly machine/OS dependent, and aren't queryable with standard c++ functions or classes (at least not I know of).

    "... or how much disk space is available to write to in a certain directory, ..."

    C++ standard library also has no notion of such thing like a filesystem or directories ...

    "... or how much L2 cache is available"

    ... and even less notion about such highly MCU specific traits1.

    C++ uses a completely abstract, machine architecture and operating system agnostic view of it's world.

    1) Thank GOD, Bjarne and the c++ standards committee for this, otherwise I'd have serious problems, to write halfway portable code for the various targets I'm facing. If it fails, and cannot be proven an error from my side violating the standards, it's most probably a bug of the actual compiler implementation. That at least hinders my co-coders squirreling out, for getting onto unnecessary and obscure micro optimization attempts :-D.


    All of the above said:

    The closest you can get, asking for some machine architecture basic traits and capabilities using the current standards, is what's supported from <cstddef>, <limits> and <type_traits> IMHO.

    Though some common 'state of the art' technologies and abstractions were adopted by the latest standard definitions (e.g. like std::thread, std::chrono or filesystem experimental library).

    0 讨论(0)
  • 2021-02-05 12:36

    how do you detect how much RAM the computer has, or how much RAM the process is using, or how much disk space is available to write to in a certain directory, or how much L2 cache is available?

    You don't. Precisely none of this is the purview of the C++ language, which describes an abstract machine.

    The only reason it tells you the number of cores available is because otherwise its multi-threading model would be close to useless, and even then it does so in an abstract way ("hardware concurrency" is not "number of physical CPUs in your desktop PC").

    0 讨论(0)
  • 2021-02-05 12:39

    Determining the amount of RAM or hard disk space available is operating-system level functionality. Because there are many different strategies available to tackle those issues, there's no platform independent way to get that information. The APIs for whatever OS you are developing for should provide functionality for determining those values.

    For example, the windows API appears to provide this function: http://msdn.microsoft.com/en-us/library/windows/desktop/aa366589%28v=vs.85%29.aspx which can help you to determine how much physical / virtual memory is available.

    Determining the amount of cache space available is a different matter, the following answer might help you: https://stackoverflow.com/a/12838695/3798126

    0 讨论(0)
提交回复
热议问题