<?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>Programming Interview Questions And Answers &#187; Synchronization</title>
	<atom:link href="http://programminginterviewquestions.com/tag/synchronization/feed/" rel="self" type="application/rss+xml" />
	<link>http://programminginterviewquestions.com</link>
	<description>Real time questions and answers collected from various interviews.</description>
	<lastBuildDate>Sat, 31 Jul 2010 03:45:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Thread Synchronization : Using Critical Section</title>
		<link>http://programminginterviewquestions.com/tutorial/thread-synchronization-using-critical-section/8987/</link>
		<comments>http://programminginterviewquestions.com/tutorial/thread-synchronization-using-critical-section/8987/#comments</comments>
		<pubDate>Wed, 18 Nov 2009 02:06:51 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MFC]]></category>
		<category><![CDATA[Tutorial]]></category>
		<category><![CDATA[Critical Section]]></category>
		<category><![CDATA[Synchronization]]></category>
		<category><![CDATA[Thread]]></category>

		<guid isPermaLink="false">http://programminginterviewquestions.com/?p=8987</guid>
		<description><![CDATA[Download source code for this project from here Using Critical Section Software development is a team effort. Unless team members cooperate with one another, synchronize their work with the rest of the team, the team can&#8217;t go far. Similarly if in a program there are several threads running, unless their activities are synchronized with one [...]]]></description>
			<content:encoded><![CDATA[<p>Download source code for this project from here <a href="http://www.kicit.com/freebies/vcpp/threads/Using_Critical_Section.zip">Using Critical Section</a></p>
<p>Software development is a team effort. Unless team members cooperate with one another, synchronize their work with the rest of the team, the team can&#8217;t go far. Similarly if in a program there are several threads running, unless their activities are synchronized with one another the disaster is not far away. Windows supports four types of objects—Critical sections, Mutexes, Events and Semaphores—that can help us to coordinate the activities of concurrently running threads in an application. Of these Critical section is the simplest to use. If the resources like variables, structures, linked lists, etc. are shared by two or more threads, critical section can serialize the accesses to these resources. However, the resources must belong to the same process as critical section does not work across the processes.</p>
<p>If a thread needs exclusive access to a resource then it can lock and unlock the same. The resource in such a case is referred to as a ‘critical section’. If thread B tries to lock a critical section that is locked by thread A, then thread B is denied an access to the critical section until thread A unlocks it.</p>
<p>Let us now see an example of critical section where names of students are stored in a <strong>CStringList</strong> object. A thread <strong>sortthread</strong> sorts the names while another thread <strong>addthread </strong>adds a new name to the list. We have blocked the thread that adds the name to the list if the sorting thread is in progress. Also, we have blocked the thread that sorts the names if addition of names is in progress.</p>
<p>Create a dialog based application called ‘critical’. Add two buttons, an edit box and a list box to the dialog. The controls, their IDs and variables associated with them are given below:</p>
<table border="1" cellspacing="0" cellpadding="0" width="474">
<tbody>
<tr>
<td width="126" valign="top"><strong>Control</strong><strong> </strong></td>
<td width="100" valign="top"><strong>ID</strong><strong> </strong></td>
<td width="134" valign="top"><strong>Variable</strong><strong> </strong></td>
<td width="137" valign="top"><strong>Category</strong><strong> </strong></td>
</tr>
<tr>
<td width="126" valign="top">Add button</td>
<td width="100" valign="top">IDC_ADD</td>
<td width="134" valign="top"></td>
<td width="137" valign="top"></td>
</tr>
<tr>
<td width="126" valign="top">Sort button</td>
<td width="100" valign="top">IDC_SORT</td>
<td width="134" valign="top"></td>
<td width="137" valign="top"></td>
</tr>
<tr>
<td width="126" valign="top">Edit box</td>
<td width="100" valign="top">IDC_ADDEDIT</td>
<td width="134" valign="top">m_addedit</td>
<td width="137" valign="top">Value</td>
</tr>
<tr>
<td width="126" valign="top">List box</td>
<td width="100" valign="top">IDC_SORTLIST</td>
<td width="134" valign="top">m_sortlist</td>
<td width="137" valign="top">Control</td>
</tr>
</tbody>
</table>
<p>The dialog template after adding the controls would appear as shown below.</p>
<p align="center"><img class="alignnone size-full wp-image-8988" title="Image1" src="http://programminginterviewquestions.com/wp-content/uploads/2009/11/Image1.gif" alt="Image1" width="276" height="266" /></p>
<p>The name entered by the user in the edit box will get added to the string list if &#8216;Add&#8217; button is clicked. The list box will show the sorted records if &#8216;Sort&#8217; button is clicked.</p>
<p>Declare a global structure in ‘criticalDlg.h’ file as given below:</p>
<blockquote><p>struct THREADPARAMS</p>
<p>{</p>
<p>CString itemstr ;</p>
<p>CListBox *list ;</p>
<p>CCriticalSection *cr ;</p>
<p>} ;</p></blockquote>
<p><em>#</em><strong>include</strong> &#8216;afxmt.h&#8217; file in &#8216;criticalDlg.h&#8217; file. This makes the <strong>CCriticalSection</strong> class available to our application. Add a <strong>private</strong> variable <strong>m_c</strong> of type <strong>CCriticalSection </strong>in <strong>CCriticalDlg</strong> class. Also add a <strong>static</strong> variable <strong>m_strlist</strong> of type <strong>CStringList</strong> to the <strong>CCriticalDlg</strong> class. Add the following statement in ‘criticalDlg.cpp’.</p>
<p>CListBox CCriticalDlg::m_sortlist ;</p>
<p>Add the following code in <strong>OnInitDialog( )</strong> function.</p>
<blockquote><p>BOOL CCriticalDlg::OnInitDialog( )</p>
<p>{</p>
<p>// Code added by AppWizard</p>
<p>CStdioFile f ;</p>
<p>CString str ;</p>
<p>if ( f.Open ( &#8220;data.txt&#8221;, CFile::modeReadWrite ) != 0 )</p>
<p>{</p>
<p>while ( f.ReadString ( str ) != NULL )</p>
<p>m_strlist.AddTail ( str ) ;</p>
<p>}</p>
<p>f.Close( ) ;</p>
<p>return TRUE ;</p>
<p>}</p></blockquote>
<p>The ‘data.txt’ file contains the names of students. Here, we have read the records from the file using <em>CStdioFile::ReadString( )</em> function and added them to the string list using <strong>CStringList::AddTail( )</strong> function.</p>
<p>Add <strong>OnSort( )</strong> handler for the &#8216;Sort&#8217; button. The code of <strong>OnSort( )</strong> function is given below:</p>
<blockquote><p>void CCriticalDlg::OnSort( )</p>
<p>{</p>
<p>THREADPARAMS *th = new THREADPARAMS ;</p>
<p>th -&gt; itemstr = &#8220;&#8221; ;</p>
<p>th -&gt; list = &amp;m_sortlist ;</p>
<p>th -&gt; cr = &amp;m_c ;</p>
<p>AfxBeginThread ( sortthread, ( LPVOID ) th ) ;</p>
<p>}</p></blockquote>
<p>Here we have initialised the elements of THREADPARAMS structure that we are passing to the <strong>sortthread( )</strong> function. We have started the thread by calling <strong>AfxBeginThread( )</strong> function. The code of the <strong>sortthread( )</strong> function is given below:</p>
<blockquote><p>UINT sortthread ( LPVOID param )</p>
<p>{</p>
<p>THREADPARAMS *th = ( THREADPARAMS* ) param ;</p>
<p>int i, j ;</p>
<p>CString l1, l2 ;</p>
<p>POSITION p1, p2 ;</p>
<p>th -&gt; cr -&gt; Lock( ) ;</p>
<p>int count = CCriticalDlg::m_strlist.GetCount( ) ;</p>
<p>for ( j = 0 ; j &lt; count ; j++ )</p>
<p>{</p>
<p>p1 = CCriticalDlg::m_strlist.GetHeadPosition( ) ;</p>
<p>for ( i = 0 ; i &lt; count &#8211; 1 ; i++ )</p>
<p>{</p>
<p>p2 = p1 ;</p>
<p>l1 = CCriticalDlg::m_strlist.GetNext ( p1 ) ;</p>
<p>l2 = CCriticalDlg::m_strlist.GetAt ( p1 ) ;</p>
<p>if ( l1 &gt; l2 )</p>
<p>{</p>
<p>CCriticalDlg::m_strlist.SetAt ( p2, l2 ) ;</p>
<p>CCriticalDlg::m_strlist.SetAt ( p1, l1 ) ;</p>
<p>}</p>
<p>}</p>
<p>}</p>
<p>th -&gt; list -&gt; ResetContent( ) ;</p>
<p>POSITION pos = CCriticalDlg::m_strlist.GetHeadPosition( ) ;</p>
<p>for ( i = 0 ; i &lt; count ; i++ )</p>
<p>th -&gt; list -&gt; AddString ( CCriticalDlg::m_strlist.GetNext ( pos ) ) ;</p>
<p>th -&gt; cr -&gt; Unlock( ) ;</p>
<p>return 0 ;</p>
<p>}</p></blockquote>
<p>Here, we have first locked the object of <strong>CStringList</strong> class using <strong>CCriticalSection::Lock( )</strong> function. Next we have sorted the list using the usual ‘bubble sort’ sorting logic. <strong>CStringList::GetNext( )</strong> function gives the string at the specified position and sets the POSITION variable to point to the next entry. <strong>CStringList::GetNext( )</strong> function gets the string at the given position. The sorted list is then displayed in the list box. After the thread is through with its job the object is unlocked using <strong>CCriticalSection::Unlock( )</strong> function.</p>
<p>Add <strong>OnAdd( )</strong> handler for the Add button and write the following code in it.</p>
<blockquote><p>void CCriticalDlg::OnAdd( )</p>
<p>{</p>
<p>UpdateData ( TRUE ) ;</p>
<p>THREADPARAMS *th = new THREADPARAMS ;</p>
<p>th -&gt; itemstr = m_addstr ;</p>
<p>th -&gt; cr = &amp;m_c ;</p>
<p>AfxBeginThread ( addthread, ( LPVOID ) th ) ;</p>
<p>}</p></blockquote>
<p><strong>UpdateData( TRUE )</strong> transfers the name from edit box to <strong>m_addstr</strong> variable. We have initialised the THREADPARAMS structure with values that we intend to pass to the <strong>addthread( )</strong> function. Next, we have started the thread by calling <strong>AfxBeginThread( )</strong> function.</p>
<p>The code of the <strong>addthread( )</strong> function is given below:</p>
<blockquote><p>UINT addthread ( LPVOID param )</p>
<p>{</p>
<p>THREADPARAMS *th = ( THREADPARAMS* ) param ;</p>
<p>th -&gt; cr -&gt; Lock( ) ;</p>
<p>if ( !th -&gt; itemstr.IsEmpty( ) )</p>
<p>CCriticalDlg::m_strlist.AddTail ( th -&gt; itemstr ) ;</p>
<p>th -&gt; cr -&gt; Unlock( ) ;</p>
<p>return 0;</p>
<p>}</p></blockquote>
<p>In this function, the critical section, i.e., <strong>m_strlist</strong> is locked using <strong>CCriticalSection::Lock( )</strong>. Now no other thread can use this object unless this thread unlocks it by calling <strong>CCriticalSection::UnLock( )</strong> function.</p>
<p>Don’t forget to declare the thread functions in ‘criticalDlg.cpp’ as given below:</p>
<blockquote><p>UINT addthread ( LPVOID param ) ;</p>
<p>UINT sortthread ( LPVOID param ) ;</p></blockquote>
<p>Finally, add the <strong>OnOK( ) </strong>handler to write back the updated string list to the file. Its code is given below:</p>
<blockquote><p>void CCriticalDlg::OnOK( )</p>
<p>{</p>
<p>CFile::Remove ( &#8220;data.txt&#8221; ) ;</p>
<p>CStdioFile f ;</p>
<p>if ( f.Open ( &#8220;data.txt&#8221;, CFile::modeCreate | CFile::modeReadWrite ) != 0 )</p>
<p>{</p>
<p>POSITION pos = m_strlist.GetHeadPosition( ) ;</p>
<p>CString str ;</p>
<p>for ( int i = 0 ; i &lt; m_strlist.GetCount( ) ; i++ )</p>
<p>{</p>
<p>str = m_strlist.GetNext ( pos ) ;</p>
<p>f.WriteString ( str ) ;</p>
<p>f.WriteString ( &#8220;\n&#8221; ) ;</p>
<p>}</p>
<p>}</p>
<p>f.Close( ) ;</p>
<p>CDialog::OnOK( ) ;</p>
<p>}</p></blockquote>
<p>Now run the program. If you click the &#8216;Sort&#8217; button and immediately follow it by clicking the Add button the <em>add </em>thread would get blocked until the <em>sort </em>thread unlocks the critical section.</p>
<p>Download source code for this project from here <a href="http://www.kicit.com/freebies/vcpp/threads/Using_Critical_Section.zip">Using Critical Section</a></p>
]]></content:encoded>
			<wfw:commentRss>http://programminginterviewquestions.com/tutorial/thread-synchronization-using-critical-section/8987/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is Multithreading?</title>
		<link>http://programminginterviewquestions.com/uncategorized/what-is-multithreading/2099/</link>
		<comments>http://programminginterviewquestions.com/uncategorized/what-is-multithreading/2099/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 04:52:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Synchronization]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://technicalcontents.com/visualc/?p=220</guid>
		<description><![CDATA[]]></description>
			<content:encoded><![CDATA[]]></content:encoded>
			<wfw:commentRss>http://programminginterviewquestions.com/uncategorized/what-is-multithreading/2099/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multi-Threading in Windows</title>
		<link>http://programminginterviewquestions.com/uncategorized/multi-threading-in-windows/2098/</link>
		<comments>http://programminginterviewquestions.com/uncategorized/multi-threading-in-windows/2098/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 04:51:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Synchronization]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://technicalcontents.com/visualc/?p=219</guid>
		<description><![CDATA[there are two ways to program with multiple threads: use the Microsoft Foundation Class (MFC) library or the C run-time library and the Win32 API. What is threading ? A thread is basically a path of execution through a program. It is also the smallest unit of execution. A thread consists of a stack, the [...]]]></description>
			<content:encoded><![CDATA[<p><!--[if !mso]&gt;  v\:* {behavior:url(#default#VML);} o\:* {behavior:url(#default#VML);} w\:* {behavior:url(#default#VML);} .shape {behavior:url(#default#VML);}  &lt;![endif]--><!--[if gte mso 9]&gt;     Normal   0         false   false   false                                 MicrosoftInternetExplorer4   &lt;![endif]--><!--[if gte mso 9]&gt;     &lt;![endif]--> <!--  /* Font Definitions */  @font-face 	{font-family:SimSun; 	panose-1:2 1 6 0 3 1 1 1 1 1; 	mso-font-alt:???????????????????????????????; 	mso-font-charset:134; 	mso-generic-font-family:auto; 	mso-font-pitch:variable; 	mso-font-signature:3 135135232 16 0 262145 0;} @font-face 	{font-family:Verdana; 	panose-1:2 11 6 4 3 5 4 4 2 4; 	mso-font-alt:Arial; 	mso-font-charset:0; 	mso-generic-font-family:swiss; 	mso-font-pitch:variable; 	mso-font-signature:536871559 0 0 0 415 0;} @font-face 	{font-family:"\@SimSun"; 	panose-1:2 1 6 0 3 1 1 1 1 1; 	mso-font-alt:"\@Arial Unicode MS"; 	mso-font-charset:134; 	mso-generic-font-family:auto; 	mso-font-pitch:variable; 	mso-font-signature:3 135135232 16 0 262145 0;}  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal 	{mso-style-parent:""; 	margin:0in; 	margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:12.0pt; 	font-family:"Times New Roman"; 	mso-fareast-font-family:SimSun;} h1 	{mso-margin-top-alt:auto; 	margin-right:0in; 	mso-margin-bottom-alt:auto; 	margin-left:0in; 	mso-pagination:widow-orphan; 	mso-outline-level:1; 	font-size:24.0pt; 	font-family:"Times New Roman";} span.linkterms 	{mso-style-name:linkterms; 	font-weight:normal;} @page Section1 	{size:8.5in 11.0in; 	margin:1.0in 1.25in 1.0in 1.25in; 	mso-header-margin:.5in; 	mso-footer-margin:.5in; 	mso-paper-source:0;} div.Section1 	{page:Section1;}  /* List Definitions */  @list l0 	{mso-list-id:334461046; 	mso-list-template-ids:491685948;} @list l0:level1 	{mso-level-number-format:bullet; 	mso-level-text:?; 	mso-level-tab-stop:.5in; 	mso-level-number-position:left; 	text-indent:-.25in; 	mso-ansi-font-size:10.0pt; 	font-family:Symbol;} @list l1 	{mso-list-id:1694261331; 	mso-list-type:hybrid; 	mso-list-template-ids:24540658 -2034183198 67698713 67698715 67698703 67698713 67698715 67698703 67698713 67698715;} @list l1:level1 	{mso-level-tab-stop:.5in; 	mso-level-number-position:left; 	text-indent:-.25in; 	mso-ansi-font-size:12.0pt; 	font-family:"Times New Roman"; 	color:windowtext;} ol 	{margin-bottom:0in;} ul 	{margin-bottom:0in;} --> <!--[if gte mso 10]&gt;   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin:0in; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;}  &lt;![endif]-->
<p class="MsoNormal"></p>
<ol style="margin-top:0;" start="1" type="1">
<li class="MsoNormal" style="color:black;"><span style="font-size:8.5pt;font-family:Verdana;">there are two ways to program      with multiple threads: use the Microsoft Foundation Class (MFC) library or      the C run-time library and the Win32 API.</span></li>
</ol>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:Verdana;color:black;"> </span></p>
<p class="MsoNormal"><span style="font-size:8.5pt;font-family:Verdana;color:black;">What is threading ?</span></p>
<p class="MsoNormal" style="margin:9.35pt 0 4.7pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">A thread is basically a path of execution through a program. It is also the smallest unit of execution. A thread consists of a stack, the state of the CPU registers, and an entry in the execution list of the system scheduler. Each thread can shares all the process&#8217;s resources.</span></p>
<p class="MsoNormal" style="margin:9.35pt 0 4.7pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">A process consists of </span></p>
<p class="MsoNormal" style="margin:9.35pt 0 4.7pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">1. one or more threads </span></p>
<p class="MsoNormal" style="margin:9.35pt 0 4.7pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">2. the code, </span></p>
<p class="MsoNormal" style="margin:9.35pt 0 4.7pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">3. data, </span></p>
<p class="MsoNormal" style="margin:9.35pt 0 4.7pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">4. other resources of a program in memory. </span></p>
<p class="MsoNormal" style="text-indent:12pt;margin:9.35pt 0 4.7pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">Ex.<span>  </span>open files, semaphores, dynamically allocated memory. </span></p>
<p class="MsoNormal" style="text-indent:12pt;margin:9.35pt 0 4.7pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">A program executes when the system scheduler gives one of its threads execution control. The scheduler determines which threads should run and when they should run. Threads of lower priority might have to wait while higher priority threads complete their tasks. On multiprocessor machines, the scheduler can move individual threads to different processors to balance the CPU load.</span></p>
<h1 style="margin:16.85pt 0 7.5pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;font-weight:normal;">Each thread in a process operates independently. Unless you make them visible to each other, the threads execute individually and are unaware of the other threads in a process. Threads sharing common resources, however, must coordinate their work by using semaphores or another method of inter-process communication. </span></h1>
<h1 style="margin:16.85pt 0 7.5pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;font-weight:normal;"> </span></h1>
<h1 style="margin:16.85pt 0 7.5pt;"><span style="font-size:11.5pt;font-family:Verdana;color:rgb(0,51,153);font-weight:normal;">Library Support – </span><span style="font-size:8.5pt;font-family:Verdana;color:black;font-weight:normal;">all versions of CRT –c run time libraries supports with the exception of the non-locking versions of some functions</span></h1>
<h1 style="margin:16.85pt 0 7.5pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;font-weight:normal;"> </span></h1>
<h1 style="margin:16.85pt 0 7.5pt;"><span style="font-size:11.5pt;font-family:Verdana;color:rgb(0,51,153);font-weight:normal;">Include Files for Multithreading</span><span style="font-size:8.5pt;font-family:Verdana;color:black;font-weight:normal;"></span></h1>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">Standard include files declare C run-time library functions as they are implemented in the libraries. If you use the Full Optimization (/Ox) or fastcall Calling Convention (/Gr) option, the compiler assumes that all functions should be called using the register calling convention. The run-time library functions were compiled using the C calling convention, and the declarations in the standard include files tell the compiler to generate correct external references to these functions.</span></p>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;"> </span></p>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><b><span style="font-size:11.5pt;font-family:Verdana;color:rgb(0,51,153);">C Run-Time Library Functions for Thread Control</span></b><span style="font-size:8.5pt;font-family:Verdana;color:black;"></span></p>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">All Win32 programs have at least one thread. Any thread can create additional threads. A thread can complete its work quickly and then terminate, or it can stay active for the life of the program.</span></p>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><span style="font-size:8.5pt;font-family:Verdana;color<br />
:black;">The LIBCMT and MSVCRT C run-time libraries provide the following functions for thread creation and termination: <span class="linkterms">_beginthread, _beginthreadex</span> and <span class="linkterms">_endthread, _endthreadex</span>.</span></p>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">The <b>_beginthread</b> and <b>_beginthreadex</b> functions create a new thread and return a thread identifier if the operation is successful. The thread terminates automatically if it completes execution, or it can terminate itself with a call to <b>_endthread</b> or <b>_endthreadex</b>.</span></p>
<table class="MsoNormalTable" style="width:98.9%;" width="98%" border="0" cellpadding="0" cellspacing="0">
<tbody>
<tr>
<td style="background:rgb(239,239,247) none repeat scroll 0 0;border-color:0 0 rgb(200,205,222);border-style:none none solid;border-width:medium medium 1pt;padding:0 3.4pt;" valign="top">
<p class="MsoNormal" style="margin:3.4pt 0;"><b><span style="font-size:13.5pt;font-family:Verdana;color:rgb(0,0,102);"><!--[if gte vml 1]&gt;                                                                                          &lt;![endif]--><!--[if !vml]--><img src="/DOCUME%7E1/shesu04/LOCALS%7E1/Temp/msohtml1/01/clip_image001.gif" alt="Note" width="10" height="10" /><!--[endif]-->Note </span></b></p>
</td>
</tr>
<tr>
<td style="background:rgb(247,247,255) none repeat scroll 0 0;border-color:white 0 rgb(213,213,211);border-style:solid none;border-width:1pt medium;padding:0 3.4pt;" valign="top">
<p class="MsoNormal" style="margin:3.4pt .7pt 5.45pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">If you are going to call C run-time routines from a program   built with Libcmt.lib, you must start your threads with the _beginthread or   _beginthreadex function. Do not use the Win32 functions ExitThread and   CreateThread. Using SuspendThread can lead to a deadlock when more than one   thread is blocked waiting for the suspended thread to complete its access to   a C run-time data structure.</span><span style="font-size:13.5pt;font-family:Verdana;color:black;"></span></p>
</td>
</tr>
</tbody>
</table>
<h1 style="margin:12.25pt 0 5.45pt;"><a name="_core_the__beginthread_function"></a><span style="font-size:11pt;font-family:Verdana;color:rgb(0,51,153);">The _beginthread and _beginthreadex Functions</span></h1>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">The <b>_beginthread</b> and <b>_beginthreadex</b> functions create a new thread. A thread shares the code and data segments of a process with other threads in the process but has its own unique register values, stack space, and current instruction address. The system gives CPU time to each thread, so that all threads in a process can execute concurrently.</span></p>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><b><span style="font-size:8.5pt;font-family:Verdana;color:black;">_beginthread</span></b><span style="font-size:8.5pt;font-family:Verdana;color:black;"> and <b>_beginthreadex</b> are similar to the CreateThread function in the Win32 API but has these differences:</span></p>
<p class="MsoNormal" style="text-indent:-.25in;margin:1.35pt 0 1.35pt 11.55pt;"><!--[if !supportLists]--><span style="font-size:10pt;font-family:Symbol;color:black;"><span>·<span style="font-family:&quot;font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;">                          </span></span></span><!--[endif]--><b><span style="font-size:8.5pt;font-family:Verdana;color:black;">_beginthread</span></b><span style="font-size:8.5pt;font-family:Verdana;color:black;"> and <b>_beginthreadex</b> let you pass multiple arguments to the thread.</span></p>
<p class="MsoNormal" style="text-indent:-.25in;margin:1.35pt 0 1.35pt 11.55pt;"><!--[if !supportLists]--><span style="font-size:10pt;font-family:Symbol;color:black;"><span>·<span style="font-family:&quot;font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;">                          </span></span></span><!--[endif]--><span style="font-size:8.5pt;font-family:Verdana;color:black;">They initialize certain C run-time library variables. This is important only if you use the C run-time library in your threads.</span></p>
<p class="MsoNormal" style="text-indent:-.25in;margin:1.35pt 0 1.35pt 11.55pt;"><!--[if !supportLists]--><span style="font-size:10pt;font-family:Symbol;color:black;"><span>·<span style="font-family:&quot;font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;">                          </span></span></span><!--[endif]--><b><span style="font-size:8.5pt;font-family:Verdana;color:black;">CreateThread</span></b><span style="font-size:8.5pt;font-family:Verdana;color:black;"> helps prov<br />
ide control over security attributes. You can use this function to start a thread in a suspended state.</span></p>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><b><span style="font-size:8.5pt;font-family:Verdana;color:black;">_beginthread</span></b><span style="font-size:8.5pt;font-family:Verdana;color:black;"> and <b>_beginthreadex</b> return a handle to the new thread if successful or an error code if there was an error.</span></p>
<h1 style="margin:12.25pt 0 5.45pt;"><a name="_core_the__endthread_function"></a><span style="font-size:11pt;font-family:Verdana;color:rgb(0,51,153);">The _endthread and _endthreadex Functions</span></h1>
<p class="MsoNormal" style="margin:6.8pt 0 3.4pt;"><span style="font-size:8.5pt;font-family:Verdana;color:black;">The _endthread function terminates a thread created by <b>_beginthread</b> (and similarly, <b>_endthreadex</b> terminates a thread created by <b>_beginthreadex</b>). Threads terminate automatically when they finish. <b>_endthread</b> and <b>_endthreadex</b> are useful for conditional termination from within a thread. A thread dedicated to communications processing, for example, can quit if it is unable to get control of the communications port.</span></p>
<div class="blogger-post-footer">VC++ FAQ&#8217;s And Interview Questions and Answers<img width='1' height='1'/></div>
]]></content:encoded>
			<wfw:commentRss>http://programminginterviewquestions.com/uncategorized/multi-threading-in-windows/2098/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Threading Questions and Answers</title>
		<link>http://programminginterviewquestions.com/uncategorized/threading-questions-and-answers/8979/</link>
		<comments>http://programminginterviewquestions.com/uncategorized/threading-questions-and-answers/8979/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 04:48:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Synchronization]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://technicalcontents.com/visualc/?p=218</guid>
		<description><![CDATA[37) What is the difference between process and thread? Ans: Process is a program in execution whereas thread is a separate path of execution in a program. 38) What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined? Ans: Multithreading is the mechanism [...]]]></description>
			<content:encoded><![CDATA[<p><!--[if gte mso 9]&gt;     Normal   0         false   false   false                                 MicrosoftInternetExplorer4   &lt;![endif]--><!--[if gte mso 9]&gt;     &lt;![endif]--> <!--  /* Font Definitions */  @font-face 	{font-family:SimSun; 	panose-1:2 1 6 0 3 1 1 1 1 1; 	mso-font-alt:???????????????????????????????; 	mso-font-charset:134; 	mso-generic-font-family:auto; 	mso-font-pitch:variable; 	mso-font-signature:3 135135232 16 0 262145 0;} @font-face 	{font-family:"Comic Sans MS"; 	panose-1:3 15 7 2 3 3 2 2 2 4; 	mso-font-charset:0; 	mso-generic-font-family:script; 	mso-font-pitch:variable; 	mso-font-signature:647 0 0 0 159 0;} @font-face 	{font-family:"\@SimSun"; 	panose-1:2 1 6 0 3 1 1 1 1 1; 	mso-font-alt:"\@Arial Unicode MS"; 	mso-font-charset:134; 	mso-generic-font-family:auto; 	mso-font-pitch:variable; 	mso-font-signature:3 135135232 16 0 262145 0;}  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal 	{mso-style-parent:""; 	margin:0in; 	margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:12.0pt; 	font-family:"Times New Roman"; 	mso-fareast-font-family:"Times New Roman"; 	color:black; 	mso-fareast-language:EN-US;} h3 	{mso-margin-top-alt:auto; 	margin-right:0in; 	mso-margin-bottom-alt:auto; 	margin-left:0in; 	mso-pagination:widow-orphan; 	mso-outline-level:3; 	font-size:13.5pt; 	font-family:"Times New Roman"; 	mso-fareast-font-family:"Times New Roman"; 	mso-fareast-language:EN-US;} p.MsoList2, li.MsoList2, div.MsoList2 	{margin-top:0in; 	margin-right:0in; 	margin-bottom:0in; 	margin-left:.5in; 	margin-bottom:.0001pt; 	text-indent:-.25in; 	mso-pagination:widow-orphan; 	font-size:12.0pt; 	font-family:"Times New Roman"; 	mso-fareast-font-family:"Times New Roman"; 	mso-fareast-language:EN-US;} p.MsoBodyText, li.MsoBodyText, div.MsoBodyText 	{margin-top:0in; 	margin-right:0in; 	margin-bottom:6.0pt; 	margin-left:0in; 	mso-pagination:widow-orphan; 	font-size:12.0pt; 	font-family:"Times New Roman"; 	mso-fareast-font-family:"Times New Roman"; 	mso-fareast-language:EN-US;} @page Section1 	{size:8.5in 11.0in; 	margin:1.0in 1.25in 1.0in 1.25in; 	mso-header-margin:.5in; 	mso-footer-margin:.5in; 	mso-paper-source:0;} div.Section1 	{page:Section1;} --> <!--[if gte mso 10]&gt;   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin:0in; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;}  &lt;![endif]--><br />
<h3 style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;font-weight:normal;">37)<span> </span>What is the difference between process and thread? </span></h3>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Ans: Process is a program in execution whereas thread is a separate path of execution in a program.</span></p>
<h3 style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;font-weight:normal;">38)<span>          </span>What is multithreading and what are the methods for inter-thread communication and what is the class in which these methods are defined?</span></h3>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Ans: Multithreading is the mechanism in which more than one thread run independent of each other within the process.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">wait (), notify () and notifyAll() methods can be used for inter-thread communication and these methods are in Object class. </span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">wait( ) : When a thread executes a call to wait( ) method, it surrenders the object lock and enters into a waiting state.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">notify( ) or notifyAll( ) : To remove a thread from the waiting state, some other thread must make a call to<span>  </span>notify( ) or notifyAll( ) method on the same object.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">39)<span>          </span>What is the class and interface in java to create thread and which is the most advantageous method?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Ans: Thread class and Runnable interface can be used to create threads and using Runnable interface is the most advantageous method to create threads because we need not extend thread class here. </span></p>
<h3 style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;font-weight:normal;">40)<span>          </span>What are the states associated in the thread?</span></h3>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Ans: Thread contains ready, running, waiting and dead states.</span></p>
<h3 style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;font-weight:normal;">41)<span>           </span>What is synchronization?</span></h3>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Ans: Synchronization is the mechanism that ensures that only one thread is accessed the resources at a time.</span></p>
<h3 style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;font-weight:normal;">42)<span>          </span>When you will synchronize a piece of your code?</span></h3>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Ans: When you expect your code will be accessed by different threads and these threads may change a particular data causing data corruption.</span></p>
<h3 style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;font-weight:normal;">43)<span>          </span>What is deadlock?</span></h3>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Ans: When two threads are waiting each other and can’t precede the program is said to be deadlock.</span></p>
<h3 style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;font-weight:normal;">44)<span>          </span>What<br />
 is daemon thread and which method is used to create the daemon thread?</span></h3>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Ans: Daemon thread is a low priority thread which runs intermittently in the back ground doing the garbage collection operation for the java runtime system. setDaemon method is used to create a daemon thread.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">40. What is runnable?.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Its an Interface through which Java implements Threads.The class can extend from any class but if it implements Runnable,Threads can be used in that particular application.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">41. What is preemptive and Non-preemptive Time Scheduling?.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Preemptive: Running tasks are given small portions of time to execute by using time-slicing.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Non-Preemptive: One task doesn’t give another task a chance to run until its finished or has normally yielded its time.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">42. What is synchronization?.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Two or more threads trying to access the same method at the same point of time leads to synchronization.If that particular method is declared as synchronized only one thread can access it at a time. Another thread can access it only if the first thread’s task is complete.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">43. What are the various thread priorities?.</span></p>
<p class="MsoList2" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">(i)<span>  </span>Min-Priority-value(1).</span></p>
<p class="MsoList2" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">(ii) Normal-Priority-value(5).</span></p>
<p class="MsoList2" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">(iii)Max-Priority-value(10).</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;"> </span></p>
<p class="MsoList2" style="margin-left:0;text-align:justify;text-indent:0;"><span style="font-size:8pt;font-family:&quot;">44.What is Inter-Thread communication?.</span></p>
<p class="MsoList2" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">To exchange information between two threads.</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;"> </span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">48.Throwable class is a sub-class of object and implements Serializable.</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">83. What is the superclass of exception?.<span>                         </span>Throwable.</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;"> </span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">82. What is the return type of interrupt method?.<span>                            </span>void.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;"> </span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">3.Why do threads block on I/O?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Threads block on i/o (that is enters the waiting state) so that other threads</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">may execute while the i/o</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Operation is performed.</span></p>
<p class="MsoNormal">
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">5. What is synchronization and why is it important?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">With respect to multithreading, synchronization is the capability to control the access of multiple threads to shared resources. Without synchronization, it is possible for one thread to modify a shared object while another thread is in the process of using or updating that object’s value. This often leads to significant errors.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">6. Can a lock be acquired on a class?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Yes, a lock can be acquired on a class. This lock is acquired on the class’s Class object.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">7. What’s new with the stop(), suspend() and resume() methods in JDK 1.2?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">The stop(), suspend() and resume() methods have been deprecated in JDK 1.2.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">12. What state does a thread enter when it terminates its processing?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">When a thread terminates its processing, it enters the dead state.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">23What is the difference between yielding and sleeping?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">When a task invokes its yield() method, it returns to the ready state. When a task invokes its sleep() method, it returns to the waiting state.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">31. What is the difference between preemptive scheduling and time slicing?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Under preemptive scheduling, the highest priority task executes until it enters the waiting or dead states or a higher priority task comes into existence.Under time slicing, a task executes for a predefined slice of time and thenreenters the pool of ready tasks. The scheduler then determines which task should execute next, based on priority and other factors.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">39. When a thread blocks on I/O, what state does it enter?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">A thread enters the waiting state when it blocks on I/O.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">43. What is a task’s priority and how is it used in scheduling?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">A task’s priority is an integer value that identifies the relative order in which it should be executed with respect to other tasks. The scheduler attempts to schedule higher priority tasks before lower priority tasks.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;<br />
font-family:&quot;">45. When a thread is created and started, what is its initial state?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">A thread is in the ready state after it has been created and started.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">53. What invokes a thread’s run() method?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">After a thread is started, via its start() method or that of the Thread class, the JVM invokes the thread’s run() method when the thread is initially executed.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">67. What method is invoked to cause an object to begin executing as a separate thread?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">The start() method of the Thread class is invoked to cause an object to begin executing as a separate thread.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">72. What is the purpose of the wait(), notify(), and notifyAll() methods?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">The wait(),notify(), and notifyAll() methods are used to provide an efficient way for threads to wait for a shared resource. When a thread executes an object’s wait() method, it enters the waiting state. It only enters the ready state after another thread invokes the object’s notify() or notifyAll() methods.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">76. What are the high-level thread states?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">The high-level thread states are ready, running, waiting, and dead.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">82. What is an object’s lock and which object’s have locks?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">An object’s lock is a mechanism that is used by multiple threads to obtain synchronized access to the object. A thread may execute a synchronized method of an object only after it has acquired the object’s lock. All objects and classes have locks. A class’s lock is acquired on the class’s Class object.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">93. What happens when a thread cannot acquire a lock on an object?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">If a thread attempts to execute a synchronized method or synchronized statement and is unable to acquire an object’s lock, it enters the waiting state until the lock becomes available.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">134. What happens when you invoke a thread’s interrupt method while it is sleeping or waiting?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">When a task’s interrupt() method is executed, the task enters the ready state.<span>  </span>The next time the task enters the running state, an InterruptedException is thrown.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">149. What state is a thread in when it is executing?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">An executing thread is in the running state.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">170. How can a dead thread be restarted?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">A dead thread cannot be restarted.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">174. What are three ways in which a thread can enter the waiting state?</span></p>
<p class="MsoNormal" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">A thread can enter the waiting state by invoking its sleep() method, by blocking on I/O, by unsuccessfully attempting to acquire an object’s lock, or by invoking an object’s wait() method. It can also enter the waiting state by invoking its (deprecated) suspend() method.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">191. What method must be implemented by all threads?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">All tasks must implement the run() method, whether they are a subclass of Thread or implement the Runnable interface.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">194. What are synchronized methods and synchronized statements?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">Synchronized methods are methods that are used to control access to an object.<span>  </span>A thread only executes a synchronized method after it has acquired the lock for the method’s object or class. Synchronized statements are similar to synchronized methods. A synchronized statement can only be execute<br />
d after a thread has acquired the lock for the object or class referenced in the synchronized statement.</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">195. What are the two basic ways in which classes that can be run as threads may be defined?</span></p>
<p class="MsoBodyText" style="text-align:justify;"><span style="font-size:8pt;font-family:&quot;">A thread class may be declared as a subclass of Thread, or it may implement the Runnable interface.</span></p>
<p class="MsoNormal">
<div class="blogger-post-footer">VC++ FAQ&#8217;s And Interview Questions and Answers<img width='1' height='1'/></div>
]]></content:encoded>
			<wfw:commentRss>http://programminginterviewquestions.com/uncategorized/threading-questions-and-answers/8979/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Multithreading and Synchronization in Win32 applications</title>
		<link>http://programminginterviewquestions.com/uncategorized/multithreading-and-synchronization-in-win32-applications/2096/</link>
		<comments>http://programminginterviewquestions.com/uncategorized/multithreading-and-synchronization-in-win32-applications/2096/#comments</comments>
		<pubDate>Wed, 16 Jul 2008 04:39:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Synchronization]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://technicalcontents.com/visualc/?p=217</guid>
		<description><![CDATA[Synchronization &#8211; Overview The concurrent execution of threads and processes in Win32 is achieved by synchronization mechanisms. Following methods/objects are used to achieve synchronization Critical Section – single process; – non kernel object Mutexes Mutually Exclusive Critical Section + Multi Processes Semaphores Semaphores – keeps trap of resource usage by no of threads Resource acquired [...]]]></description>
			<content:encoded><![CDATA[<p><!--[if gte mso 9]&gt;     Normal   0         false   false   false                                 MicrosoftInternetExplorer4   &lt;![endif]--><!--[if gte mso 9]&gt;     &lt;![endif]--> <!--  /* Font Definitions */  @font-face 	{font-family:Wingdings; 	panose-1:5 0 0 0 0 0 0 0 0 0; 	mso-font-charset:2; 	mso-generic-font-family:auto; 	mso-font-pitch:variable; 	mso-font-signature:0 268435456 0 0 -2147483648 0;} @font-face 	{font-family:SimSun; 	panose-1:2 1 6 0 3 1 1 1 1 1; 	mso-font-alt:???????????????????????????????; 	mso-font-charset:134; 	mso-generic-font-family:auto; 	mso-font-pitch:variable; 	mso-font-signature:3 135135232 16 0 262145 0;} @font-face 	{font-family:Gulim; 	panose-1:2 11 6 0 0 1 1 1 1 1; 	mso-font-alt:??; 	mso-font-charset:129; 	mso-generic-font-family:swiss; 	mso-font-pitch:variable; 	mso-font-signature:-1342176593 1775729915 48 0 524447 0;} @font-face 	{font-family:Tahoma; 	panose-1:2 11 6 4 3 5 4 4 2 4; 	mso-font-alt:Tahoma; 	mso-font-charset:0; 	mso-generic-font-family:swiss; 	mso-font-pitch:variable; 	mso-font-signature:1627421319 -2147483648 8 0 66047 0;} @font-face 	{font-family:"Arial Black"; 	panose-1:2 11 10 4 2 1 2 2 2 4; 	mso-font-charset:0; 	mso-generic-font-family:swiss; 	mso-font-pitch:variable; 	mso-font-signature:647 0 0 0 159 0;} @font-face 	{font-family:"Comic Sans MS"; 	panose-1:3 15 7 2 3 3 2 2 2 4; 	mso-font-charset:0; 	mso-generic-font-family:script; 	mso-font-pitch:variable; 	mso-font-signature:647 0 0 0 159 0;} @font-face 	{font-family:"\@Gulim"; 	panose-1:2 11 6 0 0 1 1 1 1 1; 	mso-font-charset:129; 	mso-generic-font-family:swiss; 	mso-font-pitch:variable; 	mso-font-signature:-1342176593 1775729915 48 0 524447 0;} @font-face 	{font-family:"\@SimSun"; 	panose-1:2 1 6 0 3 1 1 1 1 1; 	mso-font-alt:"\@Arial Unicode MS"; 	mso-font-charset:134; 	mso-generic-font-family:auto; 	mso-font-pitch:variable; 	mso-font-signature:3 135135232 16 0 262145 0;}  /* Style Definitions */  p.MsoNormal, li.MsoNormal, div.MsoNormal 	{mso-style-parent:""; 	margin:0in; 	margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:12.0pt; 	font-family:"Times New Roman"; 	mso-fareast-font-family:SimSun;} @page Section1 	{size:8.5in 11.0in; 	margin:1.0in 1.25in 1.0in 1.25in; 	mso-header-margin:.5in; 	mso-footer-margin:.5in; 	mso-paper-source:0;} div.Section1 	{page:Section1;}  /* List Definitions */  @list l0 	{mso-list-id:1670062491; 	mso-list-type:hybrid; 	mso-list-template-ids:1976196272 1378674092 67698691 67698693 67698689 67698691 67698693 67698689 67698691 67698693;} @list l0:level1 	{mso-level-start-at:2005; 	mso-level-number-format:bullet; 	mso-level-text:–; 	mso-level-tab-stop:.5in; 	mso-level-number-position:left; 	text-indent:-.25in; 	font-family:"Comic Sans MS"; 	mso-fareast-font-family:SimSun; 	mso-bidi-font-family:"Comic Sans MS";} ol 	{margin-bottom:0in;} ul 	{margin-bottom:0in;} --> <!--[if gte mso 10]&gt;   /* Style Definitions */  table.MsoNormalTable 	{mso-style-name:"Table Normal"; 	mso-tstyle-rowband-size:0; 	mso-tstyle-colband-size:0; 	mso-style-noshow:yes; 	mso-style-parent:""; 	mso-padding-alt:0in 5.4pt 0in 5.4pt; 	mso-para-margin:0in; 	mso-para-margin-bottom:.0001pt; 	mso-pagination:widow-orphan; 	font-size:10.0pt; 	font-family:"Times New Roman"; 	mso-ansi-language:#0400; 	mso-fareast-language:#0400; 	mso-bidi-language:#0400;}  &lt;![endif]-->
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:16pt;"></p>
<p></span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"> Synchronization &#8211; <span style="font-size:14pt;">Overview</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span> </span>The concurrent execution of threads and processes in</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Win32 is achieved by synchronization mechanisms. </p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Following methods/objects are used to achieve synchronization</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Critical Section </p>
<p class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;color:rgb(51,51,255);font-family:trebuchet ms;"><!--[if !supportLists]--><span>–<span style="font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;">        </span></span><!--[endif]--><span style="font-size:10pt;">single process; </span></p>
<p class="MsoNormal" style="margin-left:.5in;text-indent:-.25in;color:rgb(51,51,255);font-family:trebuchet ms;"><!--[if !supportLists]--><span>–<span style="font-style:normal;font-variant:normal;font-weight:normal;font-size:7pt;line-height:normal;">        </span></span><!--[endif]--><span style="font-size:10pt;">non kernel object</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Mutexes</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;"><span> </span>Mutually Exclusive</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Critical Section + Multi Processes</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Semaphores</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Semaphores – keeps trap of resource usage by no of threads</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Resource acquired by thread it does Counter ++</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Resource released by thread it does Counter&#8211;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Event Objects</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Signal Set – Reset Events</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Auro reset and manual reset</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Resource </p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">: memory location, data structure, file…</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Buffer</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Data</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Thread 1:</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">data</span><span style="font-size:10pt;"></span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Thread 2:</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"><span> </span>Critical Sections</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Critical Section</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Portion of a code that accesses a shared resource.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Critical Section Object</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Provided by Win32 API</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– used to enforce </span><span style="font-size:10pt;">mutual exclusion </span><span style="font-size:10pt;">between </span><span style="font-size:10pt;">threads within</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">a single process</span><span style="font-size:10pt;">.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Only one Thread at a time is allowed to be “inside” the</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">critical section.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Local object</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">• kernel object</span><span style="font-size:7pt;"></span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Define critical section object</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– CRITICAL_SECTION</span><span style="font-size:10pt;">critical_section;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Initialized critical section</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– VOID </span><span style="font-size:10pt;">InitializeCriticalSection </span><span style="font-size:10pt;">(LPCRITICAL_SECTION</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">lpCriticalSection);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Deleting critical section object</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– VOID </span><span style="font-size:10pt;">DeleteCriticalSection </span><span style="font-size:10pt;">(LPCRITICAL_SECTION</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">lpCriticalSection);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">2005-10-19 </span><span style="font-size:6pt;">Seong Jong Choi </span><span style="font-size:7pt;">Multithreading_CHAPTER_4-9</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Entering the critical section</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– VOID </span><span style="font-size:10pt;">EnterCriticalSection </span><span style="font-size:10pt;">(LPCRITICAL_SECTION</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">lpCriticalSection);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Leaving the critical section</p>
<p class="MsoNormal<br />
" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– VOID </span><span style="font-size:10pt;">LeaveCriticalSection </span><span style="font-size:10pt;">(LPCRITICAL_SECTION</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">lpCriticalSection);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">• Example : Linked list </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">typedef struct _Node{</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">struct _Node *next;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">int data;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">} Node;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">typedef struct _List{</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Node *head;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">CRITICAL_SECTION Critical_sec;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">} List;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">List *CreateList(){</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">List *pList = malloc(sizeof(pList));</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">pList-&gt;head = NULL;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">InitializeCriticalSection(&amp;pList-&gt;Critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">return pList;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">void DeleteList(List *pList)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">{</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">DeleteCriticalSection(&amp;pList-&gt;Critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">free(pList);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">void AddHead(List *pList, Node *node)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">{</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">EnterCriticalSection(&amp;pList-&gt;critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">node-&gt;next = pList-&gt;head;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">pList-&gt;head = node;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LeaveCriticalSection(&amp;pList-&gt;Critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Example : Linked </p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">AddHead()</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | head</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">NULL</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">List</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Void Insert(List *pList, Node *afterNode, Node *newnode){</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">EnterCriticalSection(&amp;pList-&gt;critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">if(afterNode == NULL){</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">AddHead(pList, newNode);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">else{</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">newNode-&gt;next = afterNode-&gt;next;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">afterNode-&gt;next = newNode;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<span style="font-size:9pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">LeaveCriticalSection(&amp;pList-&gt;Critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Node *next(List *pList, Node *node){</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Node* next;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">EnterCriticalSection(&amp;pList-&gt;Critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">next = node-&gt;next;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">LeaveCriticalSection(&amp;pList-&gt;Critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">return next;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Example : Linked list </p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Insert()</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">1.</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">NULL</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">AfterNode</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">newNode</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">AfterNode</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Example : Linked list </p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">*Next()</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">head Data | next Data | next NULL</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">List node node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Critical Section</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">EnterCriticalSection(pCS)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">EnterCriticalSection(pCS)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">…</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">LeaveCriticalSection(pCS)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">LeaveCriticalSection(pCS)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">2005-10-19 </span><span style="font-size:6pt;">Seong Jong Choi </span><span style="font-size:7pt;">Multithreading_CHAPTER_4-17</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Minimizing Lock Time</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Don’t lock a resource for long time !</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Never call Sleep() or any of the Wait…() APIs inside of a</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">critical section.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– How often the resource will be used and how quickly a</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">thread must release the resource.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Void Dangling Critical Sections</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– LeaveCriticalSection()</span><span style="font-size:10pt;" lang="ZH-CN">?? </span><span style="font-size:10pt;">thread</span><span style="font-size:10pt;" lang="ZH-CN">? ????</span><span style="font-size:10pt;">..?</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Critical section is not kernel object -&gt; there is no way to</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">tell if the thread currently inside a critical section is</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">still alive.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">Deadlock</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Example SwapLists() –p.82</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">void SwapLists(List *list1, List *list2){</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">List *tmp_list;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">EnterCriticalSection(list1-&gt;critical_sec);</span><span style="font-size:9pt;">//context switch</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">EnterCriticalSection(list2-&gt;critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">tmp_list = list1-&gt;head;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">list1-&gt;head = list2-&gt;head;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">list2-&gt;head = tmp_list;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LeaveCriticalSection(list1-&gt;critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LeaveCriticalSection(list2-&gt;critical_sec);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">ThreadA SwapLists(home_address_list, work_address_list);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">ThreadA SwapLists(work_address_list, home_address_list);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">Deadlock</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">head Data | next Data | next NULL</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Tmp_list Node Node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">head Data | next Data | next NULL</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">List1 Node Node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Data | next</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">head Data | next Data | next NULL</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">List2 Node Node</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Deadlock() = “The Deadly Embrace”</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Deadlock will not happen by allocating all the</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">resources you need as a single operation with</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">WaitForMultipleObjects(). (later…-&gt; P.91)</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• All-or-nothing proposition</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">The Dining Philosophers</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">The table set for the dining philosophers</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">eating</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">thinking Waiting to eat</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Let’s See</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">the Dining.c</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">Mutexes</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• MUTualExclusion</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• Only one thread at a time is allowed to own a mutex, just as</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">only one thread at a time can enter a critical section.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• A kernel object that will enforce mutual exclusion between</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">threads even if they are in different processes.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">Timeout (see Wait…() func) not</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">Between Processes Within the same process</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">Almost 100 times longer</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Mutexes Critical section</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">The comparison of the function</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">DeleteCriticalSection() CloseHandle()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LeaveCriticalSection() ReleaseMutex()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:tr ebuchet ms;"><span style="font-size:10pt;">WaitForSingleObject()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">WaitForMultipleObjects()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">MsgWaitForMultipleObjects()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">EnterCriticalSection()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">CreatMutex()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">OpenMutex()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">InitializeCriticalSection()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">CRITICAL_SECTION Mutex Kernel Object</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span> </span>Creating Mutex</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">HANDL</span><span style="font-size:10pt;">E </span><span style="font-size:10pt;">CreateMutex</span><span style="font-size:10pt;">(</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LPSECURITY_ATTRIBUTES lpAttribute,</span></p>
<p class<br />
="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">BOOL bInitialOwner,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LPCTSTR lpName</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– lpAttribute: </span><span style="font-size:8pt;">Use NULL to get the default Security Attributes.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– bInitialOwner: </span><span style="font-size:8pt;">Set to TRUE if you want the thread that called</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">CreateMutex() to own the mutex.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– lpName:</span><span style="font-size:8pt;">Any thread or process can refer to this mutex by name</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• a Mutex has a reference count. You should call CloseHandle()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– CreateMutex Reference Count++</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– CloseHandle Reference Count&#8211;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Opening Mutex</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><b><span style="font-size:9pt;">HANDLE OpenMutex(</span></b></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><b><span style="font-size:9pt;">DWORD </span></b><span style="font-size:9pt;">dwDesiredAccess, // access</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">BOOL bInheritHandle, // inheritance option</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">LPCTSTR lpName // object name );</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">– dwDesiredAccess </span><span style="font-size:9pt;">:Specifies the requested access to the mutex.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">– bInheritHandle </span><span style="font-size:9pt;">: Specifies whether the returned handle is</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">inheritable.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– lpName: Name to access Mutex.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Mutex has a reference count. You should call CloseHandle()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Locking a Mutex</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– To acquire ownership of the mutex, use one of the Win32</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Wait….() functions.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– If a thread </span><span style="font-size:10pt;">waits on a non-signaled mutex </span><span style="font-size:10pt;">then the</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">thread will </span><span style="font-size:10pt;">block</span><span style="font-size:10pt;">.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– A Mutex is signaled when no thread owns it.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Scenario (textbook p.87 side effect)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">BOOL </span><span style="font-size:10pt;">ReleaseMutex</span><span style="font-size:10pt;">( HANDLE hMutex );</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">- a thread releases ownship of the mutex by calling</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">ReleaseMutex().</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Handling Abandoned Mutexes</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– If a thread that owns a mutex exits without calling</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:treb uchet ms;"><span style="font-size:10pt;">ReleaseMutex(), the mutex is not destroyed.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Mutex -&gt; unowned, nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Wait…() -&gt; return : WAIT_ABANDONED_0</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">(~WAIT_ABANDONED_n &#8211; 1)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Letting the Philosophers Eat</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Source </span><span style="font-size:10pt;"></span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• Fixing SwapLists</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Void SwapLists(struct List *list1, struct List *list2)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">{</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">struct List *tmp_list;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">HANDLE arrhandles[2];</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">arrHandles[0] = list1-&gt;hMutex;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">arrHandles[1] = list2-&gt;hMutex;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">WaitForMultipleObjects(2, arrHandles, TRUE, INFINITE);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">tmp_list = list1-&gt;head;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">list1-&gt;head = list2-&gt;head;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">list2-&gt;head = tmp_list;</span>
</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">ReleaseMutex(arrHandles[0]);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">ReleaseMutex(arrHandles[1]);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">}</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Why Have an Initial Owner?</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– bInitialOwner of CreateMutex()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– It prevents a race condition.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">HANDLE hMutex = CreateMutex(NULL, </span><span style="font-size:8pt;">FALSE</span><span style="font-size:8pt;">, “Simple Name”); &lt;&lt;</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">int result = WaitForSingleObject(hMutex, INFINITE);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Critical Section</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">EnterCriticalSection(pCS)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">EnterCriticalSection(pCS)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">…</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">LeaveCriticalSection(pCS)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">LeaveCriticalSection(pCS)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Mutex</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">WaitforSingleObject(hMtx)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">WaitforSingleObject(hMtx)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">…</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">ReleaseMutex(hMtx)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">ReleaseMutex(hMtx)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">Semaphores</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• They are the key ingredient in solving various</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">producer/consumer </span><span style="font-size:10pt;">problems </span><span style="font-size:10pt;">where a buffer is being read</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">and written at the same time.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• A semaphore in Win32 </span><span style="font-size:10pt;">may be locked at most n times</span><span style="font-size:10pt;">, where</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">n is specified when the semaphore is created.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• EX) car rental problem</span>.</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Creating Semaphores</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">HANDL</span><span style="font-size:10pt;">E </span><span style="font-size:10pt;">Cre<br />
ateSemaphore</span><span style="font-size:10pt;">(</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LPSECURITY_ATTRIBUTES lpAttribute,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LONG InitialCount,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LONG MaximumCount,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LPCTSTR lpName</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– lpAttribute: </span><span style="font-size:8pt;">Use NULL to get the default Security Attributes.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– InitialCount: Specifies an initial count for the semaphore</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">object(0 &lt;= this &lt;= MaximemCount)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– MaximumCount: Specifies the maximum count for the</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">semaphore object</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– lpName:</span><span style="font-size:8pt;">Any thread or process can refer to this Semaphore by name</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Acquiring Locks</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– The Value of the semaphore represents the number of</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">resources currently available.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– You acquire a lock on a semaphore by using any of the Wait…()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– There is no concept of ownership (Semaphore has no owner)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">• Several Threads can lock a semaphore at the same time</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">• Semaphore can be released by any thread</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Releasing Locks</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– To release a lock, call ReleaseSemaphore()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– This function increments the semaphore’s value</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">BOOL ReleaseSemaphore(</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">HANDLE hSemaphore,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LONG lReleaseCount,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">LPLONG lpPreviousCount</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">hSemaphore </span><span style="font-size:9pt;">Handle to the semaphore</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">lReleaseCount </span><span style="font-size:9pt;">Increment the value of the semaphore</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">lpPreviousCount </span><span style="font-size:9pt;">Return the previous value of the semaphore.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Note the there is no way to get the current value.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Why Have an Initial Count?</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Initial Value of the semaphore</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Must be greater than or equal to zero and less than</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">MaximumCount</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– ReleaseSemaphore() can increment the count up to its</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">maximum</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">Event Objects</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">• The most flexible type of synchronization object in Win32 Kernel</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">Object ( signaled or nonsignaled)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">HANDLE CreateEvent(</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">LPSECURITY_ATTRIBUTES lpEventAttributes,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">BOOL bManualReset, // T=AutoReset, F=Manual Reset</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">BOOL bInitialState, // initial state; T=signaled, F=nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">LPCTSTR lpName // object name</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">);</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">SetEvent()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">Memory Operation</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">Set the event object to the signaled state</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">ResetEvent()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">Set the event object to the nonsignaled state</span></p>
<p class="MsoNorm<br />
al" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">PulseEvent()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">– Memoryless operation</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">– Manual Reset Event</span><span style="font-size:7pt;">: Set the event object to the signaled state, wake </span><span style="font-size:7pt;">everything</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">up that is currently waiting, then return to the nonsignaled state.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">– Auto Reset Event: </span><span style="font-size:7pt;">Set the event object to the signaled state, wake up </span><span style="font-size:7pt;">a single</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:7pt;">waiting thread (if any), return to the nonsignaled state.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Manual Reset Event Object: Set/Reset</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">1. Two threads are waiting for an event object (EO).</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">2. SetEvent(EO) will activate </span><span style="font-size:10pt;">all threads </span><span style="font-size:10pt;">waiting for the EO.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">3. The EO will be in nonsignaled state only after the</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">ResetEvent()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Auto Reset Event Object: Set</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">1. Two threads are waiting for an event object (EO).</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">2. SetEvent(EO) will activate </span><span style="font-size:10pt;">only one thread </span><span style="font-size:10pt;">waiting for the</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">EO, then return to nonsignaled state</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">3. If no threads are waiting, the event object&#8217;s state remains</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">signaled,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">4. Until wait (or reset()??) function.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Manual Reset Event Object: Pulse</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">1. Assume two threads are waiting for an event object (EO).</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">2. PulseEvent(EO) will </span><span style="font-size:10pt;">activate all threads </span><span style="font-size:10pt;">waiting for the EO,</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">then the EO return to the nonsignaled state.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">3. If no threads are waiting, the event object&#8217;s state remains</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Auto Reset Event Object: Pulse</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">1. If no threads are waiting, the event object&#8217;s state remains</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">2. Two threads are waiting for an event object (EO).</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">3. PulseEvent(EO) will </span><span style="font-size:10pt;">activate a single thread </span><span style="font-size:10pt;">waiting for the</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">EO, then the EO return to the nonsignaled state.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">Event Objects: Summary</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">• If threads are waiting.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">Activate </span><span style="font-size:8pt;">one</span><span style="font-size:8pt;">, then</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">Activate </span><span style="font-size:8pt;">one</span><span style="font-size:8pt;">, then &#8212; nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Auto Reset </span><span style="font-size:8pt;">stay nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">Activate <span style="font-size:8pt;">all</span><span style="font-size:8pt;">, then</span></span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:8pt;">Activate </span><span style="font-size:8pt;">all</span><span style="font-size:8pt;">, then Nonsignaled nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Manual Reset </span><span style="font-size:8pt;">stay signaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">SetEvent() ResetEvent() PulseEvent()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">• If no treads are waiting.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><b><span style="font-size:8pt;">Stay signaled, </span></b><span style="font-size:8pt;">&#8212; Stay nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Auto Reset </span><b><span style="font-size:8pt;">until wait function.</span></b></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">Manual Reset </span><span style="font-size:8pt;">Stay signaled Nonsignaled Stay nonsignaled</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">SetEvent() ResetEvent() PulseEvent()</span></p>
<p class="MsoNormal" style="color:rgb(51,51,<br />
255);font-family:trebuchet ms;"><span style="font-size:7pt;">2005-10-19 </span><span style="font-size:6pt;">Seong Jong Choi </span><span style="font-size:7pt;">Multithreading_CHAPTER_4-43</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Summary &amp; Questions</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• PulseEvent() is an memoryless operation.</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• SetEvent() is memory operation.</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Manual/auto Reset</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– No threads are waiting, and current state is signaled.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">What happens when PulseEvent()??</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;"> </span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:14pt;">Interlocked Variables</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• The simplest type of synchronization mechanism.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• Operate on a standard 32-bit long variable.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• No ability to wait</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• InterlockedIncrement(LPLONG lpTarget)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• InterlockedDecrement(LPLONG lpTarget)</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– These functions return a comparison against zero.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Use these functions to maintain your object reference count.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">(ex: AddRef(), Release())</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">Summary of Synchronization Mechanisms</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Critical Section</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Is a local object, not a kernel object</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Is fast and efficient</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Cannot be waited on more than one at a time</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Cannot determine if it was abandoned by a thread</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;">• Mutex</p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Is a kernel object</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Generates an ‘abandoned’ error if the owning thread</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">terminates</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Can be used in Wait…() calls</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Is Named, and can be opened across processes.</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">– Can only be released by the thread that owns it</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• Semaphore</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Is a kernel object</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Has no owner</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Is Nameed</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Can be released by any thread</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• Event Object</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Is a kernel object</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Is completely under program control</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Is good for designing new synchronization objects</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Does not queue up wake-up requests</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:9pt;">– Is Named</span></p>
<p class="MsoNormal" style="color:rgb(51,51,255);font-family:trebuchet ms;"><span style="font-size:10pt;">• Interlocked Variable</span></p>
<p style="color:rgb(51,51,255);font-family:trebuchet ms;" class="MsoNormal"><span style="font-size:9pt;">– Useful for reference counting</span></p>
<div class="blogger-post-footer">VC++ FAQ&#8217;s And Interview Questions and Answers<img width='1' height='1'/></div>
]]></content:encoded>
			<wfw:commentRss>http://programminginterviewquestions.com/uncategorized/multithreading-and-synchronization-in-win32-applications/2096/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>What is a Message Loop ?</title>
		<link>http://programminginterviewquestions.com/uncategorized/what-is-a-message-loop/2095/</link>
		<comments>http://programminginterviewquestions.com/uncategorized/what-is-a-message-loop/2095/#comments</comments>
		<pubDate>Thu, 10 Jul 2008 04:32:00 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VC++]]></category>
		<category><![CDATA[Synchronization]]></category>
		<category><![CDATA[Threading]]></category>

		<guid isPermaLink="false">http://technicalcontents.com/visualc/?p=216</guid>
		<description><![CDATA[while(GetMessage(&#38;Msg, NULL, 0, 0) &#62; 0) { TranslateMessage(&#38;Msg); DispatchMessage(&#38;Msg); } 1. The message loop calls GetMessage(), which looks in your message queue. If the message queue is empty your program basically stops and waits for one (it Blocks). 2. When an event occures causing a message to be added to the queue (for example the [...]]]></description>
			<content:encoded><![CDATA[<p>while(GetMessage(&amp;Msg, NULL, 0, 0) &gt; 0)<br />
{<br />
 TranslateMessage(&amp;Msg);<br />
 DispatchMessage(&amp;Msg);<br />
}</p>
<p>1. The message loop calls GetMessage(), which looks in your message queue. If the message queue is empty your program basically stops and waits for one (it Blocks).</p>
<p>2. When an event occures causing a message to be added to the queue (for example the system registers a mouse click) GetMessages() returns a positive value indicating there is a message to be processed, and that it has filled in the members of the MSG structure we passed it. It returns 0 if it hits WM_QUIT, and a negative value if an error occured.</p>
<p>3. We take the message (in the Msg variable) and pass it to TranslateMessage(), this does a bit of additional processing, translating virtual key messages into character messages. This step is actually optional, but certain things won&#8217;t work if it&#8217;s not there.</p>
<p>4. Once that&#8217;s done we pass the message to DispatchMessage(). What DispatchMessage() does is take the message, checks which window it is for and then looks up the Window  Procedure for the window. It then calls that procedure, sending as parameters the handle of the window, the message, and wParam and lParam.</p>
<p>5. In your window procedure you check the message and it&#8217;s parameters, and do whatever you want with them! If you aren&#8217;t handling the specific message, you almost always call DefWindowProc() which will perform the default actions for you (which often means it does nothing).</p>
<p>6. Once you have finished processing the message, your windows procedure returns, DispatchMessage() returns, and we go back to the beginning of the loop.<br />
This is a very important concept for windows programs. Your window procedure is not magically called by the system, in effect you call it yourself indirectly by calling DispatchMessage(). If you wanted, you could use GetWindowLong() on the window handle that the message is destined for to look up the window&#8217;s procedure and call it directly!</p>
<p>while(GetMessage(&amp;Msg, NULL, 0, 0) &gt; 0)<br />
{<br />
  WNDPROC fWndProc = (WNDPROC)GetWindowLong(Msg.hwnd, GWL_WNDPROC);<br />
  fWndProc(Msg.hwnd, Msg.message, Msg.wParam, Msg.lParam);<br />
}</p>
<p>I tried this with the previous example code, and it does work, however there are various issues such as Unicode/ANSI translation, calling timer callbacks and so forth that this method will not account for, and very likely will break all but trivial applications. So do it to try it, but don&#8217;t do it in real code <img src='http://programminginterviewquestions.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Notice that we use GetWindowLong() to retreive the window procedure associated with the window. Why don&#8217;t we just call our WndProc() directly? Well our message loop is responsible for ALL of the windows in our program, this<br />
includes things like buttons and list boxes that have their own window procedures, so we need to make sure that we call the right procedure for the window. Since more than one window can use the same window procedure, the first parameter (the handle to the window) is used to tell the window procedure which window the message is intended for. As you can see, your application spends the majority of it&#8217;s time spinning round and round in this message loop, where you joyfully send out messages to the happy windows that will process them. But what do you do when you want your program to exit? Since we&#8217;re using a while() loop, if GetMessage() were to return FALSE (aka 0), the loop would end and we would reach the end of our WinMain() thus exiting the program. This is exactly what PostQuitMessage() accomplishes. It places a WM_QUIT message into the queue, and instead of returning a<br />
positive value, GetMessage() fills in the Msg structure and returns 0. At this point, the wParam member of Msg contains the value that you passed to PostQuitMessage() and you can either ignore it, or return it from WinMain() which will then be used as the exit code when the process terminates.</p>
<p>IMPORTANT: GetMessage() will return -1 if it encounters an error. Make sure you remember this, or it will catch you out at some point&#8230; even though GetMessage() is defined as returning a BOOL, it can return values other than TRUE or FALSE, since BOOL is defined as UINT (unsigned int). The following are examples of code that may seem to work, but will not process certian conditions correctly:</p>
<p>while(GetMessage(&amp;Msg, NULL, 0, 0))<br />
while(GetMessage(&amp;Msg, NULL, 0, 0) != 0)<br />
while(GetMessage(&amp;Msg, NULL, 0, 0) == T RUE)</p>
<p>The above are all wrong! It may be of note that I used to use the first of these throughout the tutorial, since as I just mentioned, it works fine as long as GetMessage() never fails, which when your code is correct it won&#8217;t. However I failed to take into consideration that if you&#8217;re reading this, your code probably won&#8217;t be correct a lot of the time, and GetMessage() will fail at some point <img src='http://programminginterviewquestions.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  I&#8217;ve gone through and corrected this, but forgive me if I&#8217;ve missed a few spots.</p>
<p>while(GetMessage(&amp;Msg, NULL, 0, 0) &gt; 0)</p>
<p>This, or code that has the same effect should always be used. I hope you now have a better understanding of the windows message loop, if not, do not fear, things will make more<br />
sense once you have been using them for a while.
<div class="blogger-post-footer">VC++ FAQ&#8217;s And Interview Questions and Answers<img width='1' height='1'/></div>
]]></content:encoded>
			<wfw:commentRss>http://programminginterviewquestions.com/uncategorized/what-is-a-message-loop/2095/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
