使用std :: atomic_ref <T>正确使用volatile

问题描述

我无法正确使用std::atomic_ref<int>volatile来解决问题。

天真的有三种可能性:

std::atomic_ref<volatile int> ref1;
volatile std::atomic_ref<int> ref2;
volatile std::atomic_ref<volatile int> ref3;

我们何时以及何时要使用每一个?我感兴趣的用例是MMIO。

解决方法

# Config $Username = "USERNAME" $Password = "PASSWORD" $LocalFile = "C:\PATH_TO_DIR\FILNAME.EXT" #e.g. "C:\temp\somefile.txt" $RemoteFile = "ftp://PATH_TO_REMOTE_FILE" #e.g. "ftp://ftp.server.com/home/some/path/somefile.txt" try{ # Create a FTPWebRequest $FTPRequest = [System.Net.FtpWebRequest]::Create($RemoteFile) $FTPRequest.Credentials = New-Object System.Net.NetworkCredential($Username,$Password) $FTPRequest.Method = [System.Net.WebRequestMethods+Ftp]::DownloadFile $FTPRequest.UseBinary = $true $FTPRequest.KeepAlive = $false $FTPRequest.EnableSsl = $true # Send the ftp request $FTPResponse = $FTPRequest.GetResponse() # Get a download stream from the server response $ResponseStream = $FTPResponse.GetResponseStream() # Create the target file on the local system and the download buffer $LocalFileFile = New-Object IO.FileStream ($LocalFile,[IO.FileMode]::Create) [byte[]]$ReadBuffer = New-Object byte[] 1024 # Loop through the download do { $ReadLength = $ResponseStream.Read($ReadBuffer,1024) $LocalFileFile.Write($ReadBuffer,$ReadLength) } while ($ReadLength -ne 0) }catch [Exception] { $Request = $_.Exception Write-host "Exception caught: $Request" } 不同,std::atomic<T>没有std::atomic_ref<T>限定的方法。因此,对于volatile(T本身是否易失),您可能做不到很多。

鉴于quote

就像语言参考一样,atomic_ref的常量性很浅-可以通过const atomic_ref对象修改引用的值。

假设cv资格在某种程度上是一致的,那么波动性较小的atomic_ref不太可能有用,而且绝对不是您要的。

所以,你想要

volatile std::atomic_ref<T>

请注意,仅使用std::atomic_ref<volatile int> 就足够了,但是由于该标准并未对MMIO做出任何明确的保证,因此您应该查阅编译器文档和/或进行检查。它生成的代码。

以这种方式依赖std::atomic_ref<int>至少是不可移植的。具体来说,this answer及其linked paper提到了std::atomic可能不足的一些方式-您可以检查这些是否对您来说是实际的问题。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...