互斥锁可以确保对象的线程可见性,而不是明确地保护它们吗?(Can mutexes ensure thread visibility of objects while not explicitly protecting them?)
考虑以下代码,线程是否可能以不同方式看到对象的状态,尽管它们都是由相同的指针引用的?
using namespace std; class ProducerAndConsumer{ class DummyObject { public: DummyObject() { sprintf(a, "%d", rand()); } private: char a[1000]; }; mutex queue_mutex_; queue<DummyObject *> queue_; thread *t1, *t2; void Produce() { while (true) { Sleep(1); // constructing object without any explicit synchronization DummyObject *dummy = new DummyObject(); { lock_guard<mutex> guard(queue_mutex_); if (queue_.size() > 1000) { delete dummy; continue; } queue_.push(dummy); } } } void Consume() { while (true) { Sleep(1); DummyObject *dummy; { lock_guard<mutex> guard(queue_mutex_); if (queue_.empty()) continue; dummy = queue_.front(); queue_.pop(); } // Do we have dummy object's visibility issues here? delete dummy; } } public: ProducerAndConsumer() { t1 = new thread(bind(&ProducerAndConsumer::Consume, this)); t2 = new thread(bind(&ProducerAndConsumer::Produce, this)); } };
你能说这个例子是线程安全的吗? 互斥是否强制执行缓存废弃? 互斥体是否提供了比内存屏障更多的功能以及原子?
Considering the following code, is it possible that the threads may see the state of an object differently, despite they both refer by the same pointer?
using namespace std; class ProducerAndConsumer{ class DummyObject { public: DummyObject() { sprintf(a, "%d", rand()); } private: char a[1000]; }; mutex queue_mutex_; queue<DummyObject *> queue_; thread *t1, *t2; void Produce() { while (true) { Sleep(1); // constructing object without any explicit synchronization DummyObject *dummy = new DummyObject(); { lock_guard<mutex> guard(queue_mutex_); if (queue_.size() > 1000) { delete dummy; continue; } queue_.push(dummy); } } } void Consume() { while (true) { Sleep(1); DummyObject *dummy; { lock_guard<mutex> guard(queue_mutex_); if (queue_.empty()) continue; dummy = queue_.front(); queue_.pop(); } // Do we have dummy object's visibility issues here? delete dummy; } } public: ProducerAndConsumer() { t1 = new thread(bind(&ProducerAndConsumer::Consume, this)); t2 = new thread(bind(&ProducerAndConsumer::Produce, this)); } };
Could you say that this example is thread safe? Do mutexes enforce cache trashing? Do mutexes provide more functionality than memory barriers together with atomics?
原文:https://stackoverflow.com/questions/35128267
最满意答案
您还可以使用
[System.IO.File]::ReadAllText($filename)
从[System.IO.File]::ReadAllText($filename)
中获取单个字符串,包含所有换行符和其他符号。[System.IO.File]::ReadAllText("d:\test.xml").replace('1234','xxxx')|sc d:\test.xml
You can also use
[System.IO.File]::ReadAllText($filename)
to get a single string out of a file, with all newlines and other symbols.[System.IO.File]::ReadAllText("d:\test.xml").replace('1234','xxxx')|sc d:\test.xml
相关问答
更多-
TL;博士: $ListItemCollection的类型为[System.Management.Automation.PSObject] ,而不是数组。 确保它是一个数组 (例如, $ListItemCollection = @() ), +=按预期工作,即+=附加元素[1] 。 请注意, 通常输出多个项目的命令 - 如果分配给变量,则会在常规[object[]]数组中收集 - 如果命令在情境上恰好仅返回一个项目,则仅输出标量 ; 换句话说:单项输出数组会自动解包 。 因此,如果命令有可能在情境上仅返回单 ...
-
该错误非常明显: [Microsoft.ActiveDirectory.Management.ADComputer]类型的对象不包含名为“ StartsWith ”的方法。 $Computer来自哪里? 来自Get-ADComputer cmdlet ? 阅读如何列出所有AD计算机对象属性 运行$Computer | Get-Member | ft -AutoSize $Computer | Get-Member | ft -AutoSize $Computer | Get-Member | ft -Aut ...
-
尝试使用[System.IO.Compression.ZipFileExtensions]。 如果提取文件已存在,则会失败。 [cmdletbinding()] Param() Add-Type -AssemblyName System.IO.Compression.FileSystem -ErrorAction Stop $filesToExtract = @( "t.txt"; ) $archive = "$PSScriptRoot\t.zip" $dstRoot = "$PSScriptR ...
-
我找不到官方文档来支持这个,但答案来自一个相关的问题 。 错误是正确的。 你正在处理像xml这样的字符串。 如果它确实是一个xml格式的字符串,则需要首先将该字符串显式地转换为xml。 $xml = [xml](Get-AzureRmWebAppPublishingProfile -Name $webappname ` -ResourceGroupName $ResourceGroupName ` -OutputFile null) 注意:使用该cmdlet的大多数示例都使用真实文件名,例如test.xm ...
-
尽管Workbook.Close()的参数都是可选的, 但MSDN上的所有示例都提供了至少一个( SaveChanges参数),这使我相信当您不提供任何参数时,PowerShell无法识别方法签名。 你可能会有更好的运气: $wb1.Close($false) 要么 $wb1.Close($false,$null,$null) 用$true替换以保存您可能进行的任何更改 Even though the arguments to Workbook.Close() are all optional, all ...
-
您还可以使用[System.IO.File]::ReadAllText($filename)从[System.IO.File]::ReadAllText($filename)中获取单个字符串,包含所有换行符和其他符号。 [System.IO.File]::ReadAllText("d:\test.xml").replace('1234','xxxx')|sc d:\test.xml You can also use [System.IO.File]::ReadAllText($filename) to g ...
-
使用动态方法替换PowerShell字典中的占位符(Replace placeholders in PowerShell dictionary using dynamic method)[2022-01-14]
对于给定的xml数据集 ,以下脚本应按预期工作。 对于更复杂的嵌套占位符结构,请参阅### loop while ... comments(并考虑它)。 $xmldata = @"Dev Arizona%%type%% Local%%client%% A ... -
根据文档,您可以从路径字符串创建命名空间对象。 http://msdn.microsoft.com/en-us/library/windows/desktop/bb774085(v=vs.85).aspx 所以你可以这样做: $shell = New-Object -ComObject Shell.Application $colProfiles = Get-ChildItem "C:\Users\" -Name foreach ( $userProfile in $colProfiles ) { ...
-
因为你要做的是连接2个字符串,但$folder不是字符串。 它是一个system.io.directoryinfo对象,做这样的事情: $loc = $folder.FullName + "\AppData\Local\Google\Chrome\User Data\Default\Cache" $ Folder.Fullname返回一个字符串,可以连接。 Because what you are trying to do is concatenate 2 string, but $folder is n ...
-
IndexOf是string类型的方法。 我怀疑某处...doing stuff某些...doing stuff ,你在$file上操作,以致它被视为System.IO.FileInfo 。 我怀疑这部分是因为我试图在我的环境中复制它并使其正常工作: function doSomething ($file) { # This gets the index of . in 'C:\test.txt', which should be at position 7. $pos = $file. ...