std::atomic<std::string>适当地工作? [英] Does std::atomic<std::string> work appropriately?
问题描述
我正在阅读 Anthony Williams 的C++ Concurrency in Action"和第 5 章,其中讨论了新的多线程感知内存模型和原子操作,他说:
<块引用>为了将 std::atomic
用于某些用户定义的 UDT
,此类型必须具有 平凡 复制分配操作员.
据我所知,这意味着我们可以使用 std::atomic
如果以下返回 true:
std::is_trivially_copyable::value
按照这种逻辑,我们不应该使用 std::string
作为 std::atomic
的模板参数并使其正常工作.>
但是,以下代码编译并运行时具有预期的输出:
#include #include <线程>#include #include <字符串>int main(){std::atomic原子字符串;atomicString.store("TestString1");std::cout <<atomicString.load() <<std::endl;atomicString.store("TestString2");std::cout <<atomicString.load() <<std::endl;返回0;}
这是一个未定义行为的案例,它恰好按预期运行吗?
提前致谢!
标准没有指定std::atomic
的特化,所以通用的template<类型名称 T>std::atomic
适用.29.5 [atomics.types.generic] p1 状态:
有一个泛型类模板原子.模板参数 T 的类型应该是可简单复制的 (3.9).
没有声明实施必须诊断违反此要求的行为.因此,要么 (a) 您对 std::atomic<std::string>
的使用调用了未定义的行为,或者 (b) 您的实现提供了 std::atomic<std::string>
作为符合标准的扩展.
查看 std::atomic
的 MSDN 页面(http://msdn.microsoft.com/en-us/library/vstudio/hh874651.aspx),它确实明确提到了 T
的要求可以简单地复制,它没有说明任何关于 std::atomic
的具体内容.如果它是一个扩展,它是无证的.我的钱用于未定义的行为.
具体而言,17.6.4.8/1 适用(感谢 Daniel Krügler 让我直截了当):
<块引用>在某些情况下(替换函数、处理函数、对用于实例化标准库模板组件的类型的操作),C++ 标准库依赖于由 C++ 程序提供的组件.如果这些组件不满足他们的要求,则标准不会对实现提出要求.
std::string
当然不满足 std::atomic
模板参数 T
可以简单复制的要求,所以该标准对实施没有要求.作为实现质量问题,请注意 static_assert(std::is_trivially_copyable
是一个简单的诊断捕捉此违规行为.
2016-04-19 更新:我不知道更改何时发生,但 VS2015 Update 2 现在确实可以诊断 std::atomic
:
错误 C2338:atomic 要求 T 可以简单地复制.
I am reading through Anthony Williams' "C++ Concurrency in Action" and in Chapter 5, which talks about the new multithreading-aware memory model and atomic operations, and he states:
In order to use
std::atomic<UDT>
for some user-definedUDT
, this type must have a trivial copy assignment operator.
As I understand it, this means that we can use std::atomic<UDT>
if the following returns true:
std::is_trivially_copyable<UDT>::value
By this logic, we shouldn't be able to use std::string
as a template argument for std::atomic
and have it work correctly.
However, the following code compiles and runs with expected output:
#include <atomic>
#include <thread>
#include <iostream>
#include <string>
int main()
{
std::atomic<std::string> atomicString;
atomicString.store( "TestString1" );
std::cout << atomicString.load() << std::endl;
atomicString.store( "TestString2" );
std::cout << atomicString.load() << std::endl;
return 0;
}
Is this a case of undefined behaviour which just happens to behave as expected?
Thanks in advance!
The standard does not specify a specialization of std::atomic<std::string>
, so the generic template <typename T> std::atomic<T>
applies. 29.5 [atomics.types.generic] p1 states:
There is a generic class template atomic. The type of the template argument T shall be trivially copyable (3.9).
There is no statement that the implementation must diagnose violations of this requirement. So either (a) your use of std::atomic<std::string>
invokes undefined behavior, or (b) your implementation provides std::atomic<std::string>
as a conforming extension.
Looking at the MSDN page for std::atomic<T>
(http://msdn.microsoft.com/en-us/library/vstudio/hh874651.aspx), it does explicitly mention the requirement that T
be trivially copyable, and it does NOT say anything specific about std::atomic<std::string>
. If it is an extension, it's undocumented. My money is on undefined behavior.
Specifically, 17.6.4.8/1 applies (with thanks to Daniel Krügler for setting me straight):
In certain cases (replacement functions, handler functions, operations on types used to instantiate standard library template components), the C++ standard library depends on components supplied by a C++ program. If these components do not meet their requirements, the Standard places no requirements on the implementation.
std::string
certainly does not meet the std::atomic<T>
requirement that the template parameter T
be trivially copyable, so the standard places no requirements on the implementation. As a quality of implementation issue, note that static_assert(std::is_trivially_copyable<T>::value, "std::atomic<T> requires T to be trivially copyable");
is an easy diagnostic to catch this violation.
2016-04-19 Update: I don't know when the change happened, but VS2015 Update 2 does now diagnose std::atomic<std::string>
:
error C2338: atomic requires T to be trivially copyable.
这篇关于std::atomic<std::string>适当地工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!