tag:blogger.com,1999:blog-30281954275313090022023-11-15T09:13:47.913-08:00Multithreading in C#Annihttp://www.blogger.com/profile/12579498757969905831noreply@blogger.comBlogger1125tag:blogger.com,1999:blog-3028195427531309002.post-41841506210155840992010-11-01T01:49:00.000-07:002010-11-03T09:05:19.864-07:00Synchronization Primitives1. Simple blocking : <span style="font-family: Courier New;">Sleep</span>, <code>Join</code>, and <code>Task.Wait</code> are simple blocking methods. These mechanisms waits until another thread to finish or period of time to elapse.<br />
<br />
2. Locking : The standard exclusive locking constructs are lock (<code>Monitor.Enter</code>/<code>Monitor.Exit</code>),Mutex, and Spinlock . The nonexclusive locking constructs are semaphore and the reader/writer locks.<br />
<br />
3. Signaling : There are two commonly used signaling devices: Event wait handlers and <code>Monitor</code>’s <span style="font-family: Courier New;">Wait/Pulse</span> methods.<br />
<br />
4. Nonblocking synchronization constructs : C# provide the following nonblocking constructs: <span style="font-family: Courier New;">Thread.Memorybarrier,Thread.VolatileWrit</span> and the InterLocked class.<br />
<br />
<br />
<strong><span style="font-size: large;">Locking</span></strong> <br />
<br />
<strong>Lock :</strong><br />
<br />
<div xmlns="http://www.w3.org/1999/xhtml"><strong> lock</strong> 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. </div><div xmlns="http://www.w3.org/1999/xhtml"><br />
</div><div xmlns="http://www.w3.org/1999/xhtml">Syntax of <strong>lock </strong>keyword :</div><div xmlns="http://www.w3.org/1999/xhtml"><br />
</div><div xmlns="http://www.w3.org/1999/xhtml">Object lockObj= new Object();<br />
<strong>lock</strong> (lockObj) <br />
{<br />
// code section<br />
}</div><div xmlns="http://www.w3.org/1999/xhtml">Lock calls Enter at the beginning of the block and Exit at the end of the block. <br />
<br />
<div xmlns="http://www.w3.org/1999/xhtml"><strong>DO's</strong> and <strong>DONT's</strong> :</div><div xmlns="http://www.w3.org/1999/xhtml"><br />
</div><div xmlns="http://www.w3.org/1999/xhtml">* Avoid locking on <strong>public </strong>types.</div><div xmlns="http://www.w3.org/1999/xhtml">* Avoid locking on instances which is not in your code control.</div><div xmlns="http://www.w3.org/1999/xhtml">* Better to lock on <strong>private</strong> objects</div><div xmlns="http://www.w3.org/1999/xhtml">* Lock on <strong>private static</strong> object variable to protect data common to all instances.</div></div><div xmlns="http://www.w3.org/1999/xhtml"></div><div xmlns="http://www.w3.org/1999/xhtml"><strong>Monitor : </strong></div><div xmlns="http://www.w3.org/1999/xhtml"></div><div xmlns="http://www.w3.org/1999/xhtml"> <strong>Monitor</strong> class also works same as lock in which it provides a mechanism that synchronizes access to objects.But it uses The <strong>Enter</strong> method allows one and only one thread to proceed into the following statements; all other threads are blocked until the executing thread calls <strong>Exit</strong>. It Monitor is unbound, which means it can be called directly from any context. This class is under <strong>System.Threading</strong> namespace.</div><div xmlns="http://www.w3.org/1999/xhtml"><br />
Code snippet shows Monitor class: <br />
<br />
Object lockObj= new Object();<br />
<br />
Monitor.Enter(lockObj);<br />
<span style="color: blue;">try</span><br />
{<br />
DoSomething();<br />
}<br />
<span style="color: blue;">finally</span><br />
{<br />
Monitor.Exit(lockObj);<br />
}<br />
<br />
<strong>Mutex:</strong><br />
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 <strong>mutex</strong> class.<br />
<br />
<a href="http://www.amazon.com/s/?ie=UTF8&tag=multit09-20&link_code=btl&camp=213689&creative=392969&search-alias=software&field-keywords=" target="_blank"><strong><span style="color: purple;">Click here to Download Softwares</span></strong></a><strong><span style="color: purple;"><img alt="" border="0" height="1" src="http://www.assoc-amazon.com/e/ir?t=multit09-20&l=btl&camp=213689&creative=392969&o=1&a=" style="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;" width="1" /></span></strong></div>Annihttp://www.blogger.com/profile/12579498757969905831noreply@blogger.com1