<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/'><id>tag:blogger.com,1999:blog-18337532.post113090707992234985..comments</id><updated>2009-07-15T09:45:20.795+07:00</updated><title type='text'>Comments on Truong Hong Thi on .NET: C# Interlocked</title><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://thith.blogspot.com/feeds/113090707992234985/comments/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html'/><author><name>Truong Hong Thi</name><uri>http://www.blogger.com/profile/15249069123623107491</uri><email>noreply@blogger.com</email></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>9</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-18337532.post-1044681105378066668</id><published>2009-07-15T09:45:20.795+07:00</published><updated>2009-07-15T09:45:20.795+07:00</updated><title type='text'>The World Leading  wow power leveling and wow gold...</title><content type='html'>The World Leading  &lt;a href="http://www.superpowerleveling.com" rel="nofollow"&gt;wow power leveling&lt;/a&gt; and &lt;a href="http://www.lowxx.com" rel="nofollow"&gt;wow gold&lt;/a&gt; wow power leveling</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/1044681105378066668'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/1044681105378066668'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1247625920795#c1044681105378066668' title=''/><author><name>gfh</name><uri>http://www.blogger.com/profile/01167054760851222153</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-18337532.post-165528282459882809</id><published>2009-06-07T20:38:00.298+07:00</published><updated>2009-06-07T20:38:00.298+07:00</updated><title type='text'>Thanks Truong for your reply.</title><content type='html'>Thanks Truong for your reply.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/165528282459882809'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/165528282459882809'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1244381880298#c165528282459882809' title=''/><author><name>Jehanzeb</name><uri>http://www.blogger.com/profile/02974760191105434732</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-18337532.post-2295825577003878767</id><published>2009-06-07T15:08:13.361+07:00</published><updated>2009-06-07T15:08:13.361+07:00</updated><title type='text'>Hi Jehanzeb,

Because all accesses to isGateOpen a...</title><content type='html'>Hi Jehanzeb,&lt;br /&gt;&lt;br /&gt;Because all accesses to isGateOpen are already atomic, in order to make MyManualResetEvent thread-safe regardless of memory model, simply add &amp;quot;volatile&amp;quot; keyword to the declaration of MyManualResetEvent&amp;#39;s isGateOpen.&lt;br /&gt;&lt;br /&gt;private volatile Boolean isGateOpen;&lt;br /&gt;&lt;br /&gt;The &amp;quot;volatile&amp;quot; keyword prevents optimizations such as reordering or caching, and it is optimal in this situation.&lt;br /&gt;&lt;br /&gt;For the purpose of understanding the construct implementation, your second class, ThreadSafeManualResetEvent, is correct.&lt;br /&gt;However, I suggest to change WaitOne like this:&lt;br /&gt;&lt;br /&gt;public void WaitOne()&lt;br /&gt;{&lt;br /&gt;    int gateState = 0;&lt;br /&gt;    while (Interlocked.Exchanged(ref gateState, this.isGateOpen) == 0)&lt;br /&gt;        Thread.Sleep(1000); // &amp;quot;Spin-Sleeping!&amp;quot;&lt;br /&gt;}</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/2295825577003878767'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/2295825577003878767'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1244362093361#c2295825577003878767' title=''/><author><name>Truong Hong Thi</name><uri>http://www.blogger.com/profile/15249069123623107491</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00479434843416697005'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-18337532.post-14650841374154918</id><published>2009-06-07T01:26:26.421+07:00</published><updated>2009-06-07T01:26:26.421+07:00</updated><title type='text'>Hi,

I am trying to get better understanding of th...</title><content type='html'>Hi,&lt;br /&gt;&lt;br /&gt;I am trying to get better understanding of threading and synchronization constructs. So I decided to implement some on the synchronization constructs myself. Can you review my implementations and validate if they are correct?&lt;br /&gt;&lt;br /&gt;Ignore the spin-wait logic, I know it&amp;#39;s not optimal. As I said, the purpose if to understand the construct implementation.&lt;br /&gt;&lt;br /&gt;public sealed class MyManualRestEvent&lt;br /&gt;    {&lt;br /&gt;        private Boolean isGateOpen;&lt;br /&gt;&lt;br /&gt;        public MyManualRestEvent(Boolean initialState)&lt;br /&gt;        {&lt;br /&gt;            this.isGateOpen = initialState;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void ReSet()&lt;br /&gt;        {&lt;br /&gt;            //It&amp;#39;s not thread-safe&lt;br /&gt;            this.isGateOpen = false;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void Set()&lt;br /&gt;        {&lt;br /&gt;            //It&amp;#39;s not thread-safe&lt;br /&gt;            this.isGateOpen = true;&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void WaitOne()&lt;br /&gt;        {&lt;br /&gt;            //It&amp;#39;s not thread-safe&lt;br /&gt;            while (!this.isGateOpen) Thread.Sleep(1000); // &amp;quot;Spin-Sleeping!&amp;quot;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt; public sealed class ThreadSafeManualResetEvent&lt;br /&gt;    {&lt;br /&gt;        // It can possibly have only two false, 0 and 1.&lt;br /&gt;        // 0 - Gate is closed. Thread can not enter critical section.&lt;br /&gt;        // 1.- Gate is open. Waiting threads can enter the critical section.&lt;br /&gt;        private Int32 isGateOpen;&lt;br /&gt;&lt;br /&gt;        public ThreadSafeManualResetEvent(Boolean initialState)&lt;br /&gt;        {&lt;br /&gt;            Interlocked.Exchange(ref this.isGateOpen, initialState ? 1 : 0);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void ReSet()&lt;br /&gt;        {&lt;br /&gt;            Interlocked.Exchange(ref this.isGateOpen, 0);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void Set()&lt;br /&gt;        {&lt;br /&gt;            Interlocked.Exchange(ref this.isGateOpen, 1);&lt;br /&gt;        }&lt;br /&gt;&lt;br /&gt;        public void WaitOne()&lt;br /&gt;        {&lt;br /&gt;            while (Interlocked.CompareExchange(ref this.isGateOpen, this.isGateOpen, this.isGateOpen) == 0) &lt;br /&gt;                Thread.Sleep(1000); // &amp;quot;Spin-Sleeping!&amp;quot;&lt;br /&gt;        }&lt;br /&gt;    }&lt;br /&gt;&lt;br /&gt;Regards,&lt;br /&gt;Jehanzeb</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/14650841374154918'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/14650841374154918'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1244312786421#c14650841374154918' title=''/><author><name>Jehanzeb</name><uri>http://www.blogger.com/profile/02974760191105434732</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-18337532.post-733903679893276871</id><published>2008-10-09T00:50:00.000+07:00</published><updated>2008-10-09T00:50:00.000+07:00</updated><title type='text'>How about using a delegate to create the Singleton...</title><content type='html'>How about using a delegate to create the Singleton instance? you could have the delegate executed only if the singleton reference is null, to ensure it is only instantiated once.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/733903679893276871'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/733903679893276871'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1223488200000#c733903679893276871' title=''/><author><name>davsm</name><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-18337532.post-8125040224362875253</id><published>2008-02-22T17:22:00.000+07:00</published><updated>2008-02-22T17:22:00.000+07:00</updated><title type='text'>Arguments to method invocations are always evaluat...</title><content type='html'>Arguments to method invocations are always evaluated before the method is invoked.  So the new singleton will always be created.  Late-bound evaluation of arguments is a whole topic in itself.&lt;BR/&gt;&lt;BR/&gt;We're also discussing this subject over at http://www.janus-net.de/2008/01/10/double-checked-locking-in-c/.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/8125040224362875253'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/8125040224362875253'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1203675720000#c8125040224362875253' title=''/><author><name>David Turner</name><uri>http://www.blogger.com/profile/14738491011019732316</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-18337532.post-7071701995827046267</id><published>2007-12-16T15:12:00.000+07:00</published><updated>2007-12-16T15:12:00.000+07:00</updated><title type='text'>I think "no operation is performed" means value is...</title><content type='html'>I think "no operation is performed" means value is not stored in the destination. A new instance is always created to pass to the CompareExchange Method, no matter what CompareExchange does.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/7071701995827046267'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/7071701995827046267'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1197792720000#c7071701995827046267' title=''/><author><name>Thi</name><uri>http://www.blogger.com/profile/15249069123623107491</uri><email>noreply@blogger.com</email><gd:extendedProperty xmlns:gd='http://schemas.google.com/g/2005' name='OpenSocialUserId' value='00479434843416697005'/></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-18337532.post-1258093181389425362</id><published>2007-07-04T22:23:00.000+07:00</published><updated>2007-07-04T22:23:00.000+07:00</updated><title type='text'>Related to last sentence (using Interlocked.Compar...</title><content type='html'>Related to last sentence (using Interlocked.CompareExchange Method): I think that new SingletonClass object won't be created every time (even if "singleton" is not null). It will be created only in case when value of "singleton" equals null. In MSDN pages related to entire family of this method, "Remarks" section says: "If comparand and location1 are equal, then value is stored in the destination. Otherwise, no operation is performed." I hope that "not operation is performed" assumes and no creating a new instance of singleton class (which would be totally unnecessary). &lt;BR/&gt;&lt;BR/&gt;At the end of this article http://msdn2.microsoft.com/en-us/library/f857xew0(VS.71).aspx there's code equivalent to Interlocked.CompareExchange Method.</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/1258093181389425362'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/1258093181389425362'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1183562580000#c1258093181389425362' title=''/><author><name>Bojan Komazec</name><uri>http://www.blogger.com/profile/05437236807206825832</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry><entry><id>tag:blogger.com,1999:blog-18337532.post-113090992406927420</id><published>2005-11-02T12:38:00.000+07:00</published><updated>2005-11-02T12:38:00.000+07:00</updated><title type='text'>Are there any other  blogs about your tropic?     ...</title><content type='html'>Are there any other  blogs about your tropic?                                                                                                                                -----------------------------------------------------------------------------&lt;A HREF="imitrex-supplier.blogspot.com" REL="nofollow"&gt;imitrex-supplier prescription &lt;/A&gt;</content><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/113090992406927420'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/18337532/113090707992234985/comments/default/113090992406927420'/><link rel='alternate' type='text/html' href='http://thith.blogspot.com/2005/11/c-interlocked.html?showComment=1130909880000#c113090992406927420' title=''/><author><name>sonatate1</name><uri>http://www.blogger.com/profile/11720718481005376237</uri><email>noreply@blogger.com</email></author><thr:in-reply-to xmlns:thr='http://purl.org/syndication/thread/1.0' href='http://thith.blogspot.com/2005/11/c-interlocked.html' ref='tag:blogger.com,1999:blog-18337532.post-113090707992234985' source='http://www.blogger.com/feeds/18337532/posts/default/113090707992234985' type='text/html'/></entry></feed>