⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 csdn_文档中心_win32多线程程序设计学习笔记(第四章 下 第五章).htm

📁 csdn10年中间经典帖子
💻 HTM
📖 第 1 页 / 共 5 页
字号:
            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">EVENT</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">Manual(</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">)</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">Auto(</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">)</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">;</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">EVENT</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"><SPAN 
            lang=EN-US><FONT face=宋体><FONT 
            size=2>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face=宋体><FONT 
            size=2><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: black">Manual[</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">]</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">Auto[</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">]</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; 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 
            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>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN 
            style="COLOR: red; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">值得注意:</SPAN><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: black">PulseEvent()</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">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: black">P124</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">semaphore</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 
            style="COLOR: black"><FONT face=宋体><FONT 
            size=2>&nbsp;<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: black">semaphore,Interlocked</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 
            style="COLOR: black"><FONT face=宋体><FONT 
            size=2>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><SPAN 
            style="COLOR: fuchsia; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">下面进入第五章:</SPAN><SPAN 
            lang=EN-US style="COLOR: fuchsia"><o:p></o:p></SPAN></FONT></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B><SPAN 
            lang=EN-US><FONT face=宋体><FONT 
            size=2>&nbsp;<o:p></o:p></FONT></FONT></SPAN></B></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如何强制结束一个线程?</SPAN></B><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>API</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">函数,立刻便找到一个结束线程的函数―――――</SPAN><SPAN 
            lang=EN-US>TerminateThread ( ) ; </SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">用它吗?</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'">噢!<B>不要使用它</B>。被</SPAN><FONT 
            face=宋体><SPAN lang=EN-US>TerminateThread ( )</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">强制结束的线程可能会有造成以下后果:</SPAN></FONT></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo2; tab-stops: list 18.0pt"><FONT 
            face=宋体><FONT size=2><SPAN lang=EN-US>1、<SPAN 
            style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">没有机会在自己结束前释放自己所使用的资源</SPAN></FONT></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo2; tab-stops: list 18.0pt"><FONT 
            face=宋体><FONT size=2><SPAN lang=EN-US>2、<SPAN 
            style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可能引起内存泄漏</SPAN></FONT></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt 18pt; TEXT-INDENT: -18pt; mso-list: l0 level1 lfo2; tab-stops: list 18.0pt"><FONT 
            face=宋体><FONT size=2><SPAN lang=EN-US>3、<SPAN 
            style="FONT: 7pt 'Times New Roman'">&nbsp; </SPAN></SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">如果这个线程被结束时处于一个</SPAN><SPAN 
            lang=EN-US>critical section</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">之中,那么该</SPAN><SPAN 
            lang=EN-US>critical section</SPAN><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">将因此永远处于锁定状态。</SPAN></FONT></FONT></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><SPAN 
            lang=EN-US><FONT face=宋体><FONT 
            size=2>&nbsp;<o:p></o:p></FONT></FONT></SPAN></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT size=2><B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">那么如何强制结束一个线程?</SPAN></B><SPAN 
            style="FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">书上给出了一个例子,以后强制结束一个线程就靠它了。</SPAN></FONT></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 face=宋体><FONT 
            size=2><B><SPAN lang=EN-US 
            style="COLOR: green">//</SPAN></B><B><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></B></FONT></FONT></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B><SPAN 
            lang=EN-US><FONT face=宋体><FONT size=2>HANDLE hExitEvent = null 
            ;<o:p></o:p></FONT></FONT></SPAN></B></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face=宋体><FONT 
            size=2><B><SPAN lang=EN-US 
            style="COLOR: green">//</SPAN></B><B><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></B></FONT></FONT></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B><SPAN 
            lang=EN-US><FONT face=宋体><FONT size=2>DWORD WINAPI ThreadFun ( 
            LPVOID p )<o:p></o:p></FONT></FONT></SPAN></B></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B><SPAN 
            lang=EN-US><FONT face=宋体><FONT 
            size=2>{<o:p></o:p></FONT></FONT></SPAN></B></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B><SPAN 
            lang=EN-US><FONT face=宋体><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN>for (<SPAN style="mso-spacerun: yes">&nbsp; </SPAN>int I =0 ; 
            I &lt; 1000000; I++ )<o:p></o:p></FONT></FONT></SPAN></B></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><B><SPAN 
            lang=EN-US><FONT face=宋体><FONT size=2><SPAN 
            style="mso-tab-count: 1">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
            </SPAN>{<o:p></o:p></FONT></FONT></SPAN></B></P>
            <P class=MsoNormal style="MARGIN: 0cm 0cm 0pt"><FONT face=宋体><FONT 
            size=2><B><SPAN lang=EN-US><SPAN 
            style="mso-tab-count: 2">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
            style="COLOR: green">//</SPAN></SPAN></B><B><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></B></FONT></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><FONT 
            face=宋体><FONT size=2><B><SPAN lang=EN-US 
            style="COLOR: green">/*<SPAN style="mso-spacerun: yes">&nbsp; 
            </SPAN></SPAN></B><B><SPAN 
            style="COLOR: green; FONT-FAMILY: 宋体; mso-ascii-font-family: 'Times New Roman'; mso-hansi-font-family: 'Times New Roman'">可能在这里大家有点疑惑</SPAN><SPAN 
            style="COLOR: green"> </SPAN></B><B><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">SetEvent()</SPAN></B><B><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></B></FONT></FONT></P>
            <P class=MsoNormal 
            style="MARGIN: 0cm 0cm 0pt 21pt; TEXT-INDENT: 21pt"><FONT 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -