<?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/' xmlns:blogger='http://schemas.google.com/blogger/2008' xmlns:georss='http://www.georss.org/georss' xmlns:gd="http://schemas.google.com/g/2005" xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-3028195427531309002</id><updated>2024-10-19T23:13:06.305-07:00</updated><title type='text'>Multithreading in C#</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://csharp-multithreading.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3028195427531309002/posts/default?redirect=false'/><link rel='alternate' type='text/html' href='http://csharp-multithreading.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Anni</name><uri>http://www.blogger.com/profile/12579498757969905831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>1</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>25</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-3028195427531309002.post-4184150621015584099</id><published>2010-11-01T01:49:00.000-07:00</published><updated>2010-11-03T09:05:19.864-07:00</updated><title type='text'>Synchronization Primitives</title><content type='html'>1. Simple blocking : &lt;span style=&quot;font-family: Courier New;&quot;&gt;Sleep&lt;/span&gt;, &lt;code&gt;Join&lt;/code&gt;, and &lt;code&gt;Task.Wait&lt;/code&gt; are simple blocking methods.&amp;nbsp;These mechanisms waits until another thread to finish or period of time to elapse.&lt;br /&gt;
&lt;br /&gt;
2.&amp;nbsp;Locking&amp;nbsp;:&amp;nbsp;&amp;nbsp;The standard exclusive locking constructs are lock&amp;nbsp;(&lt;code&gt;Monitor.Enter&lt;/code&gt;/&lt;code&gt;Monitor.Exit&lt;/code&gt;),Mutex, and Spinlock&amp;nbsp;. The nonexclusive locking constructs are semaphore and the reader/writer locks.&lt;br /&gt;
&lt;br /&gt;
3. Signaling : There are two commonly used signaling devices:&amp;nbsp;Event wait handlers&amp;nbsp;and &lt;code&gt;Monitor&lt;/code&gt;’s&amp;nbsp;&lt;span style=&quot;font-family: Courier New;&quot;&gt;Wait/Pulse&lt;/span&gt; methods.&lt;br /&gt;
&lt;br /&gt;
4. Nonblocking synchronization constructs : C# provide the following nonblocking constructs: &lt;span style=&quot;font-family: Courier New;&quot;&gt;Thread.Memorybarrier,Thread.VolatileWrit&lt;/span&gt; and the&amp;nbsp;InterLocked class.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;&lt;span style=&quot;font-size: large;&quot;&gt;Locking&lt;/span&gt;&lt;/strong&gt;&amp;nbsp;&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Lock :&lt;/strong&gt;&lt;br /&gt;
&lt;br /&gt;
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;strong&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; lock&lt;/strong&gt; ensures that one thread does not enter a critical section of code while another thread is in the critical section. If another thread attempts to enter a locked code, it will wait, block, until the object is released. Using lock keyword only one thread can access section of code at a time and once it finishes then only another thread in a queue will be accessed. &lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;Syntax of &lt;strong&gt;lock &lt;/strong&gt;keyword :&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;Object lockObj= new Object();&lt;br /&gt;
&lt;strong&gt;lock&lt;/strong&gt; (lockObj) &lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; // code section&lt;br /&gt;
}&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;Lock calls Enter at the beginning of the block and Exit at the end of the block. &lt;br /&gt;
&lt;br /&gt;
&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;strong&gt;DO&#39;s&lt;/strong&gt; and &lt;strong&gt;DONT&#39;s&lt;/strong&gt; :&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;br /&gt;
&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;* Avoid locking on &lt;strong&gt;public &lt;/strong&gt;types.&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;* Avoid locking on instances which is not in your code control.&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;* Better to lock on &lt;strong&gt;private&lt;/strong&gt; objects&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;* Lock on &lt;strong&gt;private static&lt;/strong&gt; object variable to protect data common to all instances.&lt;/div&gt;&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;strong&gt;Monitor : &lt;/strong&gt;&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;strong&gt;Monitor&lt;/strong&gt; class also works same as lock in which it provides a mechanism that synchronizes access to objects.But it uses The&amp;nbsp;&lt;strong&gt;Enter&lt;/strong&gt;&amp;nbsp;method allows one and only one thread to proceed into the following statements; all other threads are blocked until the executing thread calls&amp;nbsp;&lt;strong&gt;Exit&lt;/strong&gt;. It Monitor is unbound, which means it can be called directly from any context. This class is under &lt;strong&gt;System.Threading&lt;/strong&gt; namespace.&lt;/div&gt;&lt;div xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;&lt;br /&gt;
Code snippet shows Monitor class: &lt;br /&gt;
&lt;br /&gt;
Object lockObj= new Object();&lt;br /&gt;
&lt;br /&gt;
Monitor.Enter(lockObj);&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;try&lt;/span&gt;&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; DoSomething();&lt;br /&gt;
}&lt;br /&gt;
&lt;span style=&quot;color: blue;&quot;&gt;finally&lt;/span&gt;&lt;br /&gt;
{&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp; Monitor.Exit(lockObj);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&lt;strong&gt;Mutex:&lt;/strong&gt;&lt;br /&gt;
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; A mutex is similar to a monitor; it prevents the simultaneous execution of a block of code by more than one thread at a time.Unlike monitors, however, a mutex can be used to synchronize threads across processes.It represents by the &lt;strong&gt;mutex&lt;/strong&gt; class.&lt;br /&gt;
&lt;br /&gt;
&lt;a href=&quot;http://www.amazon.com/s/?ie=UTF8&amp;amp;tag=multit09-20&amp;amp;link_code=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;search-alias=software&amp;amp;field-keywords=&quot; target=&quot;_blank&quot;&gt;&lt;strong&gt;&lt;span style=&quot;color: purple;&quot;&gt;Click here to Download Softwares&lt;/span&gt;&lt;/strong&gt;&lt;/a&gt;&lt;strong&gt;&lt;span style=&quot;color: purple;&quot;&gt;&lt;img alt=&quot;&quot; border=&quot;0&quot; height=&quot;1&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=multit09-20&amp;amp;l=btl&amp;amp;camp=213689&amp;amp;creative=392969&amp;amp;o=1&amp;amp;a=&quot; style=&quot;border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; margin: 0px; padding-bottom: 0px! important; padding-left: 0px! important; padding-right: 0px! important; padding-top: 0px! important;&quot; width=&quot;1&quot; /&gt;&lt;/span&gt;&lt;/strong&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://csharp-multithreading.blogspot.com/feeds/4184150621015584099/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://csharp-multithreading.blogspot.com/2010/11/synchronization-primitives.html#comment-form' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/3028195427531309002/posts/default/4184150621015584099'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/3028195427531309002/posts/default/4184150621015584099'/><link rel='alternate' type='text/html' href='http://csharp-multithreading.blogspot.com/2010/11/synchronization-primitives.html' title='Synchronization Primitives'/><author><name>Anni</name><uri>http://www.blogger.com/profile/12579498757969905831</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='https://img1.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry></feed>