<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Tobin Titus &#187; synchronization</title>
	<atom:link href="http://tobint.com/blog/tag/synchronization/feed/" rel="self" type="application/rss+xml" />
	<link>http://tobint.com</link>
	<description>You Are What You Don&#039;t Automate</description>
	<lastBuildDate>Fri, 03 Feb 2012 23:05:52 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>SynchronizationAttribute snake-oil</title>
		<link>http://tobint.com/blog/synchronizationattribute-snake-oil/</link>
		<comments>http://tobint.com/blog/synchronizationattribute-snake-oil/#comments</comments>
		<pubDate>Sun, 26 Feb 2006 11:00:20 +0000</pubDate>
		<dc:creator>tobint</dc:creator>
				<category><![CDATA[technical]]></category>
		<category><![CDATA[dotnet]]></category>
		<category><![CDATA[remoting]]></category>
		<category><![CDATA[synchronization]]></category>

		<guid isPermaLink="false">http://beta.tobint.com/?p=393</guid>
		<description><![CDATA[I have now been asked twice why you wouldn&#8217;t just use the [Synchronization] attribute when you want to make a class thread-safe. I figured I&#8217;d share what little information I have on this topic to you since I happen to have an answer. (as opposed to &#8220;the&#8221; answer which I never have). As the saying [...]
Related posts:<ol>
<li><a href='http://tobint.com/blog/thread-safe-generic-dictionary/' rel='bookmark' title='Thread-Safe Generic Dictionary'>Thread-Safe Generic Dictionary</a></li>
<li><a href='http://tobint.com/blog/unsafe-thread-safety/' rel='bookmark' title='Unsafe thread safety'>Unsafe thread safety</a></li>
<li><a href='http://tobint.com/blog/thread-switching-overhead/' rel='bookmark' title='Thread Switching Overhead'>Thread Switching Overhead</a></li>
</ol>]]></description>
			<content:encoded><![CDATA[<div class="tweetmeme_button" style="float: right; margin-left: 10px;">
			<a href="http://api.tweetmeme.com/share?url=http%3A%2F%2Ftobint.com%2Fblog%2Fsynchronizationattribute-snake-oil%2F"><br />
				<img src="http://api.tweetmeme.com/imagebutton.gif?url=http%3A%2F%2Ftobint.com%2Fblog%2Fsynchronizationattribute-snake-oil%2F&amp;source=tobint&amp;style=normal&amp;service=bit.ly&amp;b=2" height="61" width="50" /><br />
			</a>
		</div>
<p><P>I have now been asked twice why you wouldn&#8217;t just use the <FONT face="Courier New" size=2>[Synchronization]</FONT> attribute when you want to make a class thread-safe. I figured I&#8217;d share what little information I have on this topic to you since I happen to have <EM>an </EM>answer. (as opposed to &#8220;<EM>the</EM>&#8221; answer which I <EM>never</EM> have).</P><br />
<P>As the saying goes, &#8220;There is no such thing as a free lunch.&#8221;  I never really understood this saying as a kid.  I grew up in a poor neighborhood and got &#8220;free lunches&#8221; all the time. Being <EM>financially challenged</EM>, I used to laugh and say &#8220;well, this is free&#8221;.  As I&#8217;ve gotten older, and much more financially stable, I realize that my tax dollars are paying for the lunches for other kids in other neighborhood &#8212; apparently they are eating very well.</P><br />
<P>The same holds true for this special attribute.  If you may think you are getting thread-safty for free, I have some snake-oil to sell you.  There are several problems with the SynchronizationAttribute.  Take a look at the required signature for use of the synchronization attribute:</P><br />
<P><FONT style="BACKGROUND-COLOR: #ffffff" face="Courier New" size=2>[Synchronization]<BR>public class SuperSensativeClass : <BR>   System.Runtime.Remoting.Contexts.ContextBoundObject<BR>{<BR>   // Implementation here<BR>}</FONT></P><br />
<P>First, the attribute requires that you inherit from <FONT face="Courier New" size=2>ContextBoundObject</FONT> &#8212; wasting your single-inheritance chain.  If you wanted it in your object your most primitive object in the inheritance chain would have to derive from this class, and then what if you didn&#8217;t want some other child classes to be context bound?</P><br />
<P>Second, suppose your class has several methods which don&#8217;t have thread-sensative data. What happens there?  The CLR will still lock those methods since it has no idea what is thread-volatile and what isn&#8217;t.  This can severely hamper performance.</P><br />
<P>If you are looking at thread-safety, you are likely trying to optimize the perceived performance of your application using multi-threading.  The SynchronizationAttribute can actually have the exact opposite effect.  Long story short, learn to use the synchronization primatives of System.Threading.</P></p>
<p>Related posts:<ol>
<li><a href='http://tobint.com/blog/thread-safe-generic-dictionary/' rel='bookmark' title='Thread-Safe Generic Dictionary'>Thread-Safe Generic Dictionary</a></li>
<li><a href='http://tobint.com/blog/unsafe-thread-safety/' rel='bookmark' title='Unsafe thread safety'>Unsafe thread safety</a></li>
<li><a href='http://tobint.com/blog/thread-switching-overhead/' rel='bookmark' title='Thread Switching Overhead'>Thread Switching Overhead</a></li>
</ol></p>]]></content:encoded>
			<wfw:commentRss>http://tobint.com/blog/synchronizationattribute-snake-oil/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

