与std :: atomic类型的比较语义 [英] Comparison semantics with std::atomic types

查看:76
本文介绍了与std :: atomic类型的比较语义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图找到定义Tstd::atomic的比较语义的位置.

I'm trying to find where the comparison semantics for the type T with std::atomic is defined.

我知道,除了整数类型的内置专门知识之外,T可以是任何TriviallyCopyable类型.但是,像compare_and_exchange_X这样的操作如何知道如何比较 T的实例?

I know that beside the builtin specializations for integral types, T can be any TriviallyCopyable type. But how do operations like compare_and_exchange_X know how to compare an instance of T?

我想他们必须简单地对用户定义的对象(如memcmp)进行逐字节比较,但是我看不出在标准中明确提到了什么.

I imagine they must simply do a byte by byte comparison of the user defined object (like a memcmp) but I don't see where in the standard this is explicitly mentioned.

所以,假设我有:

struct foo
{
  std::uint64_t x;
  std::uint64_t y;
};

当我调用std::atomic<foo>::compare_and_exchange_weak()时,编译器如何知道如何比较两个std::atomic<foo>实例?

How does the compiler know how to compare two std::atomic<foo> instances when I call std::atomic<foo>::compare_and_exchange_weak()?

推荐答案

在草案n3936中,memcmp语义在第29.6.5节中得到了明确描述.

In draft n3936, memcmp semantics are explicitly described in section 29.6.5.

注意:例如,atomic_compare_exchange_strong的作用是 if(memcmp(对象,预期,sizeof(*对象))== 0) memcpy(object,& desired,sizeof(* object)); 别的 memcpy(expected,object,sizeof(* object));

Note: For example, the effect of atomic_compare_exchange_strong is if (memcmp(object, expected, sizeof(*object)) == 0) memcpy(object, &desired, sizeof(*object)); else memcpy(expected, object, sizeof(*object));

注意:如果基础类型具有填充位,陷阱位或备用表示形式,则比较和交换操作的memcpymemcmp语义可能会导致比较与operator==相等的值的比较失败具有相同的值.

Note: The memcpy and memcmp semantics of the compare-and-exchange operations may result in failed comparisons for values that compare equal with operator== if the underlying type has padding bits, trap bits, or alternate representations of the same value.

至少从n3485起就出现了这种措辞.

That wording has been present at least since n3485.

请注意,只有memcmp(p1, p2, sizeof(T)) != 0compare_and_exchange_weak有意义(保证失败). memcmp(p1, p2, sizeof(T)) == 0允许但不能保证成功.

Note that only memcmp(p1, p2, sizeof(T)) != 0 is meaningful to compare_and_exchange_weak (failure guaranteed). memcmp(p1, p2, sizeof(T)) == 0 allows but does not guarantee success.

这篇关于与std :: atomic类型的比较语义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆