【问题标题】:what is the format specifier for atomic<uint64_t> datatype in C++?C++ 中 atomic<uint64_t> 数据类型的格式说明符是什么?
【发布时间】:2020-06-24 09:45:45
【问题描述】:

我正在使用 Ubuntu 虚拟机并用 C++ 编写一个项目。 我无法在 C++ 中为 atomic 数据类型添加格式说明符。

【问题讨论】:

  • 格式说明符?你的意思是printf 等?
  • atomic&lt;uint64_t&gt; 看起来像 C++ 类型而不是 C。
  • 在 c 中,原子类型如下所示:en.cppreference.com/w/c/language/atomic
  • 请向我们展示您正在尝试做的具体示例,并告诉我们它在哪些方面不起作用。
  • 嗨@Hulk - 是的,它适用于 printf,我正在使用 C++

标签: c++ linux ubuntu


【解决方案1】:

使用.load()uint64_t 从原子中取出,然后使用PRIu64 将其打印为常规uint64_t

#include <atomic>
#include <cinttypes>
#include <cstdint>
#include <cstdio>

int main()
{
    std::atomic<std::uint64_t> x = 42;
    std::printf("x = %" PRIu64 "\n", x.load());
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-10
    • 2018-07-18
    • 1970-01-01
    • 2012-10-06
    • 2015-12-08
    • 2017-11-23
    • 2013-06-22
    • 2013-06-22
    相关资源
    最近更新 更多