与std :: atomic类型的比较语义 [英] Comparison semantics with std::atomic types
问题描述
我试图找到定义T
与std::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));
和
注意:如果基础类型具有填充位,陷阱位或备用表示形式,则比较和交换操作的
memcpy
和memcmp
语义可能会导致比较与operator==
相等的值的比较失败具有相同的值.
Note: The
memcpy
andmemcmp
semantics of the compare-and-exchange operations may result in failed comparisons for values that compare equal withoperator==
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)) != 0
对compare_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屋!