std :: atomic< std :: string>工作适当? [英] Does std::atomic<std::string> work appropriately?

查看:2328
本文介绍了std :: atomic< std :: string>工作适当?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在阅读Anthony Williams的C ++ Concurrency in Action,在第5章中,我们将讨论新的多线程感知内存模型和原子操作,他说:


为了对某些用户定义的 UDT std :: atomic< UDT>


根据我的理解,这个类型必须有一个这意味着如果下面的结果为真,我们可以使用 std :: atomic< UDT>

  std :: is_trivially_copyable< UDT> :: value 

不应该能够使用 std :: string 作为 std :: atomic 的模板参数,



但是,以下代码使用预期输出进行编译和运行:

  #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;
}

这是一个未定义行为的情况, / p>

提前感谢!

解决方案

标准没有指定专门化 std :: atomic< std :: string> ,所以通用 template< typename T> std :: atomic< T> 适用。 29.5 [atomics.types.generic] p1状态:


有一个通用类模板原子。模板参数T的类型应该是可复制的(3.9)。


没有声明实现必须诊断需求。所以要么使用 std :: atomic< std :: string> 调用未定义的行为,或者(b)你的实现提供 std: :atomic< std :: string> 作为合适的扩展。



查看MSDN页面 std :: atomic< T> http://msdn.microsoft.com/ en-us / library / vstudio / hh874651.aspx ),它确实明确提到了 T 是可以复制的要求,它没有说什么具体的 std :: atomic< std :: string> 。如果它是一个扩展,它是无证的。



具体来说,17.6.4.8/1适用():


p>在某些情况下(替换函数,处理函数,用于实例化标准库模板组件的类型操作),C ++标准库取决于C ++程序提供的组件。如果这些组件不满足其要求,则该标准对实施没有要求。


std ::字符串当然不满足模板参数 T std :: atomic< T> c>需要可复制,所以标准对实现没有要求。作为实现的质量问题,请注意 static_assert(std :: is_trivially_copyable< T> :: value,std :: atomic< T>要求T是可以复制的);






2016-04-19更新:我不知道什么时候更改发生,但VS2015更新2现在诊断 std :: atomic< std :: string>


错误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-defined UDT, 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&lt; std :: string&gt;工作适当?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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