📄 csdn_文档中心_win32多线程程序设计学习笔记(第四章 下 第五章).htm
字号:
document.write(date);
// -->
</SCRIPT>
</B> </TD></TR>
<TR bgColor=#999999>
<TD colSpan=3 height=1></TD></TR></TBODY></TABLE>
<TABLE border=0 width=770>
<TBODY>
<TR>
<TD align=middle bgColor=#fafafa class=td1 vAlign=top width=150><BR>
<SCRIPT src=""></SCRIPT>
</TD>
<TD align=middle width=620>
<TABLE bgColor=#eeeeee border=0 cellPadding=0 cellSpacing=0 width=600>
<TBODY>
<TR bgColor=#ffffff>
<TD align=middle height=10 width=50></TD>
<TD align=right><A href="http://www.csdn.net/">CSDN</A> - <A
href="http://www.csdn.net/develop/">文档中心</A> - <FONT
color=#003399>Visual C++</FONT> </TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399 height=10><FONT
color=#ffffff>标题</FONT></TD>
<TD><B> WIN32多线程程序设计学习笔记(第四章
下 第五章)</B> chinani(原作) </TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR>
<TR>
<TD align=middle bgColor=#003399><FONT color=#ffffff>关键字</FONT></TD>
<TD width=500> 多线程,,学习笔记</TD></TR>
<TR>
<TD align=middle height=5></TD>
<TD align=middle width=500></TD></TR></TBODY></TABLE><!--文章说明信息结束//-->
<TABLE border=0 width=600>
<TBODY>
<TR>
<TD align=left><BR>
<P align=center class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center; mso-outline-level: 1"><FONT
face=宋体><FONT size=2><SPAN lang=EN-US>WIN32</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">多线程程序设计学习笔记(第四章</SPAN>
<SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下 第五章)</SPAN></FONT></FONT></P>
<P align=center class=MsoNormal
style="MARGIN: 0cm 0cm 0pt; TEXT-ALIGN: center"><SPAN
lang=EN-US><FONT face=宋体><FONT size=2> <?xml:namespace prefix =
o ns = "urn:schemas-microsoft-com:office:office"
/><o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">终于又看</SPAN><FONT
face=宋体><SPAN lang=EN-US><<WIN32</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">多线程程序设计</SPAN><SPAN
lang=EN-US>>></SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">了;开卷有益,今天看了第四章后半部分,感觉颇有收获;以前对</SPAN><SPAN
lang=EN-US style="COLOR: red">EVENT</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个核心对象,用起来总是心中没底,看了</SPAN><SPAN
lang=EN-US style="COLOR: red">EVENT</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的介绍也是似懂非懂的。哈!今天看了以后,真有神助,我觉得应该是搞懂了</SPAN><SPAN
lang=EN-US>(</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">自以为事</SPAN><SPAN
lang=EN-US
style="FONT-FAMILY: Wingdings; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'; mso-char-type: symbol; mso-symbol-font-family: Wingdings"><SPAN
style="mso-char-type: symbol; mso-symbol-font-family: Wingdings">J</SPAN></SPAN><SPAN
lang=EN-US>)</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。仔细想想,应该是记笔记的功劳,用脑筋写过的东西还真是不容易忘记咧!!!不但印象深刻,而且助我理解了</SPAN><SPAN
lang=EN-US style="COLOR: red">EVENT</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。</SPAN><SPAN
lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
lang=EN-US><FONT face=宋体><FONT
size=2> <o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT
size=2>好了,闲话少说,进入正题吧!</FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'"><FONT
size=2>从第三章以来,每一章都必不可少的用到了一个重要的函数,知道是什么吗?</FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><B><SPAN
style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">――――</SPAN><SPAN
lang=EN-US style="COLOR: red"><FONT face=宋体>WaitFor….
()</FONT></SPAN></B><B><SPAN
style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">系列</SPAN><SPAN
lang=EN-US style="COLOR: red"><o:p></o:p></SPAN></B></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face=宋体><FONT
size=2><SPAN lang=EN-US style="COLOR: green">//</SPAN><SPAN
style="COLOR: green; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">快速复习</SPAN><SPAN
lang=EN-US style="COLOR: green"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">判断一个线程是否结束:</SPAN><SPAN
lang=EN-US style="COLOR: blue"><FONT face=宋体>WaitForSingleObject (
HANDLE hthred …. );<o:p></o:p></FONT></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">判断是否能够进入关键区域<SPAN
style="COLOR: black">:</SPAN></SPAN><SPAN lang=EN-US
style="COLOR: blue"><FONT face=宋体>WaitForSingleObject ( hMutex….)
;<o:p></o:p></FONT></SPAN></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
lang=EN-US><FONT face=宋体><FONT
size=2> <o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face=宋体><FONT
size=2><B><SPAN lang=EN-US
style="COLOR: red">Wait….()</SPAN></B><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">会在核心对象被激发时返回,各种核心对象对激发的定义不甚相同,对于</SPAN><SPAN
lang=EN-US style="COLOR: blue">hthred</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">而言,线程结束,意味着核心对象被激发;对于</SPAN><SPAN
lang=EN-US style="COLOR: black">hMutex</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">而言</SPAN><SPAN
lang=EN-US style="COLOR: black">, hMutex</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">不再被其它任何线程使用,意味着核心对象被激发。</SPAN><B><SPAN
style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">反正对于各种核心对象而言,一定是有某种场景的出现使得核心对象被激发,除了</SPAN><SPAN
lang=EN-US style="COLOR: red">EVENT</SPAN></B><B><SPAN
style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个核心对象。</SPAN></B></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN lang=EN-US
style="COLOR: black"><FONT face=宋体><FONT
size=2> <o:p></o:p></FONT></FONT></SPAN></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">对于</SPAN><FONT
face=宋体><SPAN lang=EN-US style="COLOR: red">EVENT</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">这个核心对象而言,它的激发状态完全由程序来控制,也就是说,由自己来控制</SPAN><SPAN
lang=EN-US style="COLOR: red">EVENT</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">的激发或未激发状态</SPAN><SPAN
lang=EN-US>( </SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">通过</SPAN><SPAN
lang=EN-US style="COLOR: blue">SetEvent() , ResetEvent()</SPAN><SPAN
lang=EN-US> )</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">。当<B><SPAN
style="COLOR: teal">线程</SPAN></B></SPAN><B><SPAN lang=EN-US
style="COLOR: teal">1</SPAN></B><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">因</SPAN><SPAN
style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">调用</SPAN><SPAN
lang=EN-US style="COLOR: blue">Wait…(</SPAN><SPAN lang=EN-US
style="COLOR: red">hEvent)</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">而被阻塞后,一定是某个线程调用了</SPAN><SPAN
lang=EN-US style="COLOR: blue">SetEvent( </SPAN><SPAN lang=EN-US
style="COLOR: red">hEvent </SPAN><SPAN lang=EN-US
style="COLOR: blue">)</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">使</SPAN><SPAN
lang=EN-US style="COLOR: red">hEvent</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">被设为激发状态,从而使</SPAN><B><SPAN
style="COLOR: teal; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">线程</SPAN><SPAN
lang=EN-US style="COLOR: teal">1</SPAN></B><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">被解除阻塞继续向下运行</SPAN><SPAN
lang=EN-US style="COLOR: black">,</SPAN><SPAN
style="COLOR: black; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">具体的运用参见下表:</SPAN><SPAN
lang=EN-US style="COLOR: black"><o:p></o:p></SPAN></FONT></FONT></P>
<P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN
lang=EN-US><FONT face=宋体><FONT
size=2> <o:p></o:p></FONT></FONT></SPAN></P>
<TABLE border=1 cellPadding=0 cellSpacing=0
style="BORDER-BOTTOM: medium none; BORDER-COLLAPSE: collapse; BORDER-LEFT: medium none; BORDER-RIGHT: medium none; BORDER-TOP: medium none; mso-border-alt: solid windowtext .5pt; mso-padding-alt: 0cm 5.4pt 0cm 5.4pt">
<TBODY>
<TR>
<TD
style="BACKGROUND-COLOR: transparent; BORDER-BOTTOM: windowtext 0.5pt solid; BORDER-LEFT: windowtext 0.5pt solid; BORDER-RIGHT: windowtext 0.5pt solid; BORDER-TOP: windowtext 0.5pt solid; PADDING-BOTTOM: 0cm; PADDING-LEFT: 5.4pt; PADDING-RIGHT: 5.4pt; PADDING-TOP: 0cm; WIDTH: 65.65pt"
vAlign=top width=88>
<P align=center class=MsoNormal
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -