<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">Hello Miguel,</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">I see your point. Even worse, it's a bit ambiguous of what guarantees does the volatile field make. For example in <a href="https://msdn.microsoft.com/en-us/library/x13ttww7.aspx">MSDN</a>  nothing is mentioned about fences or memory re-orders. In that sense, Mono is correct. However, in ECMA 335 they mention acquire-release semantics such as the ones in the Volatile class you mentioned.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">One this to consider is that if the Volatile field does not have these semantics, then<b> lazy initialization</b> that relies on a volatile field and a lock ( <a href="http://csharpindepth.com/Articles/General/Singleton.aspx">double-check locking</a> ) is <b>broken </b>in Mono for iOS and Android, because there is a chance that a half created object is viewed by another thread. The way the volatile field is supposed to help, is explained <a href="https://msdn.microsoft.com/en-us/magazine/jj883956.aspx">in this post</a>. </div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">The only way to make this work right now is using the Volatile class, but most probably someone would expect the volatile field to work.</div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)"><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif;color:rgb(68,68,68)">Best,</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Jul 6, 2016 at 9:24 PM, Miguel de Icaza <span dir="ltr"><<a href="mailto:miguel@microsoft.com" target="_blank">miguel@microsoft.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">







<div bgcolor="white" lang="EN-US" link="blue" vlink="purple">
<div>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Hello,<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">I am not convinced that this is a bug worth fixing.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">I think this requires some thinking.  While this might have been the intended visible behavior from C#, this predates the extensive use of C# beyond the x86 platform.   I believe this is
 why the Volatile APIs were introduced.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Consder the documentation for it:<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><a href="https://msdn.microsoft.com/en-us/library/gg712971(v=vs.110).aspx" target="_blank">https://msdn.microsoft.com/en-us/library/gg712971(v=vs.110).aspx</a><u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">If the language/runtime already provided this support, there would be no need for these volatile methods in the first place.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri">Miguel.<u></u><u></u></span></p>
<p class="MsoNormal"><span style="font-size:11.0pt;font-family:Calibri"><u></u> <u></u></span></p>
<div style="border:none;border-top:solid #b5c4df 1.0pt;padding:3.0pt 0in 0in 0in">
<p class="MsoNormal"><b><span style="font-family:Calibri;color:black">From: </span>
</b><span style="font-family:Calibri;color:black"><<a href="mailto:mono-devel-list-bounces@lists.ximian.com" target="_blank">mono-devel-list-bounces@lists.ximian.com</a>> on behalf of Rodrigo Kumpera <<a href="mailto:kumpera@gmail.com" target="_blank">kumpera@gmail.com</a>><br>
<b>Date: </b>Wednesday, July 6, 2016 at 1:27 PM<br>
<b>To: </b>petrakeas <<a href="mailto:petrakeas@gmail.com" target="_blank">petrakeas@gmail.com</a>><br>
<b>Cc: </b>"<a href="mailto:mono-devel-list@lists.ximian.com" target="_blank">mono-devel-list@lists.ximian.com</a>" <<a href="mailto:mono-devel-list@lists.ximian.com" target="_blank">mono-devel-list@lists.ximian.com</a>><br>
<b>Subject: </b>Re: [Mono-dev] Volatile fields don't enforce acquire - release semantics like Volatile.Read() and Volatile.Write()<u></u><u></u></span></p>
</div><div><div class="h5">
<div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
<blockquote style="border:none;border-left:solid #b5c4df 4.5pt;padding:0in 0in 0in 4.0pt;margin-left:3.75pt;margin-right:0in">
<div>
<div>
<div>
<p class="MsoNormal">Yes, it looks like a bug.<u></u><u></u></p>
</div>
<div>
<p class="MsoNormal"><u></u> <u></u></p>
<div>
<p class="MsoNormal">On Wed, Jul 6, 2016 at 11:13 AM, petrakeas <<a href="mailto:petrakeas@gmail.com" target="_blank">petrakeas@gmail.com</a>> wrote:<u></u><u></u></p>
<blockquote style="border:none;border-left:solid #cccccc 1.0pt;padding:0in 0in 0in 6.0pt;margin-left:4.8pt;margin-right:0in">
<p class="MsoNormal">According to C#  specification<br>
<<a href="https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2fms228593.aspx&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=66AJc2tU2gcy4vutTkC%2b4bPl3MxAnAiXd4POGNZ3ivA%3d" target="_blank">https://msdn.microsoft.com/en-us/library/ms228593.aspx</a>> 
 :<br>
<br>
•       A read of a volatile field is called a volatile read. A volatile read has<br>
“acquire semantics”; that is, it is guaranteed to occur prior to any<span style="font-family:MingLiU"><br>
</span>references to memory that occur after it in the instruction sequence.<span style="font-family:MingLiU"><br>
</span>•       A write of a volatile field is called a volatile write. A volatile write<span style="font-family:MingLiU"><br>
</span>has “release semantics”; that is, it is guaranteed to happen after any<br>
memory references prior to the write instruction in the instruction<br>
sequence.<br>
<br>
The spec presents  an example<br>
<<a href="https://na01.safelinks.protection.outlook.com/?url=https%3a%2f%2fmsdn.microsoft.com%2fen-us%2flibrary%2faa645755(v%3dvs.71).aspx&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=cFpmsRLE5a248vj3svbpsmOBouE%2bOxE%2fwDMWjd0YkhU%3d" target="_blank">https://msdn.microsoft.com/en-us/library/aa645755(v=vs.71).aspx</a>> 
  where<br>
one thread writes "data" on a non volatile variable and "publishes" the<br>
result by writing on a volatile variable that acts as a flag. The other<br>
thread checks the volatile flag and if set, it accesses the non-volatile<br>
variable that is now *guaranteed* to contain the data.<br>
<br>
It seems that Mono 4.4 (the one used in Xamarin) does not enforce these<br>
semantics or in other words does not prevent memory re-ordering in Android<br>
and iOS that have relaxed memory models due to their CPU.<br>
<br>
I have created an a test that reproduces the problem in iOS and Android<br>
Program.cs <<a href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fmono.1490590.n4.nabble.com%2ffile%2fn4668111%2fProgram.cs&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=H7V6%2bpq4jV8Kw7QdgZMVJRav%2b1XovSCuIY3PgRFaJrk%3d" target="_blank">http://mono.1490590.n4.nabble.com/file/n4668111/Program.cs</a>> 
 .<br>
<br>
If the access to the volatile field is replaced by Volatile.Read() and<br>
Volatile.Write(), then no-problems occur. It seems that Volatile.Read() and<br>
Volatile.Write() implement half fences in Mono, but the volatile keyword<br>
does not.<br>
<br>
Is this a bug?<br>
<br>
<br>
<br>
<br>
--<br>
View this message in context: <a href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2fmono.1490590.n4.nabble.com%2fVolatile-fields-don-t-enforce-acquire-release-semantics-like-Volatile-Read-and-Volatile-Write-tp4668111.html&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=sqJVi9saxf7EEGpn6Wpf%2bFEeZX5yCpzK4%2b38x670OEw%3d" target="_blank">
http://mono.1490590.n4.nabble.com/Volatile-fields-don-t-enforce-acquire-release-semantics-like-Volatile-Read-and-Volatile-Write-tp4668111.html</a><br>
Sent from the Mono - Dev mailing list archive at Nabble.com.<br>
_______________________________________________<br>
Mono-devel-list mailing list<br>
<a href="mailto:Mono-devel-list@lists.ximian.com" target="_blank">Mono-devel-list@lists.ximian.com</a><br>
<a href="https://na01.safelinks.protection.outlook.com/?url=http%3a%2f%2flists.ximian.com%2fmailman%2flistinfo%2fmono-devel-list&data=01%7c01%7cmiguel%40microsoft.com%7cf3c960accdeb43d8500208d3a5c2d9ae%7c72f988bf86f141af91ab2d7cd011db47%7c1&sdata=Sb1mXUpzvfBCP0RJh%2bB2orCGoBH3eV8Z8CY10t1NbC0%3d" target="_blank">http://lists.ximian.com/mailman/listinfo/mono-devel-list</a><u></u><u></u></p>
</blockquote>
</div>
<p class="MsoNormal"><u></u> <u></u></p>
</div>
</div>
</div>
</blockquote>
</div></div></div>
</div>

</blockquote></div><br><br clear="all"><div><br></div>-- <br><div class="gmail_signature" data-smartmail="gmail_signature"><div dir="ltr"><div><div dir="ltr"><span><font face="verdana, sans-serif"><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><font size="2" color="#666666">Petros Douvantzis</font></span></p><p style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><font size="2" color="#666666">Co-founder</font></span></p><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><font size="2" color="#666666">Horizon Video Technologies</font></span></p><p dir="ltr" style="line-height:1.15;margin-top:0pt;margin-bottom:0pt"><span style="text-decoration:underline;vertical-align:baseline;white-space:pre-wrap;background-color:transparent"><font size="2" color="#666666"><a href="http://horizon.camera" target="_blank">horizon.camera</a></font></span><br></p></font></span><br></div></div></div></div>
</div>