📄 ch27.htm
字号:
<HTML>
<HEAD>
<TITLE>Special Edition Using Visual C++ 5 - Chapter 27</TITLE>
<LINK REL="Next" HREF="ch28.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch28.htm">
<LINK REL="Previous" HREF="ch26.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/ch26.htm"></HEAD>
<BODY BGCOLOR="#FFFFFF" TEXT="#000000">
<H2><B>Chapter 27</B></H2>
<H2><B>Multitasking with Windows Threads</B></H2>
<hr>
<P>When using Windows 95 (and other modern operating systems), you know that you can run several programs simultaneously. This ability is called <I>multitasking.</I> What you may not know is that many of today's operating systems also allow
<I>threads,</I> which are separate processes that are notcomplete applications. A thread is a lot like a subprogram. An application can create several threads—several different flows of execution—and run them concurrently. Threads give you the
ability to have multitasking inside multitasking. The user knows that he or she can run several applications at a time. The programmer knows that each application can run several threads at a time. In this chapter, you'll learn how to create and manage
threads in your applications.</P>
<ul>
<li> <B>How to Create and Run Threads</B></P>
<P> Writing a thread and getting it started is easy under MFC. In fact, one function call is all it takes to get your thread running.</P>
<li> <B>About Inter-Thread Communication</B></P>
<P> Although threads are like self-contained subprograms, they still need to communicate with other threads, including the main program. There are several ways of doing this, the best of which is Windows messages and event objects.</P>
<li> <B>About Synchronizing Threads with Critical Sections</B></P>
<P> You often have to ensure that data can be accessed by only one thread at a time. Critical sections help you accomplish this type of data protection.</P>
<li> <B>How to Use Mutexes to Synchronize Threads</B></P>
<P> Mutexes are a lot like critical sections in that they enable you to guard data against thread corruption.</P>
<li> <B>About Using Semaphores to Manage Multiple Resource Accesses</B></P>
<P> Semaphores count the number of threads that access a resource and limit those accesses to a given maximum number of threads.</P>
</ul>
<H3><B>Understanding Simple Threads</B></H3>
<P>A thread is a path of execution through a program. In a multi-threaded program, each thread has its own stack, and operates independently of any other threads that may be running within the same program. MFC distinguishes between <I>UI threads</I>,
which have a message pump and typically perform user interface tasks, and <I>worker threads</I>, which do not.</P>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>Any application always has at least one thread, which is the program's primary or main thread. You can start and stop as many additional threads as you need, but the main thread keeps running as long as the application is active.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>To create a thread using MFC, all you have to do is write a function that you wish to run parallel with the rest of your application then call <font color="#008000">AfxBeginThread()</font> to start a thread that will execute your function. The thread
remains active as long as the thread's function is executing: When the thread function exits, the thread is destroyed. A simple call to <font color="#008000">AfxBeginThread()</font> looks like this:</P>
<pre><font color="#008000">AfxBeginThread(ProcName, param, priority);</font></pre>
<P>In the previous line, <font color="#008000">ProcName</font> is the name of the thread's function, <font color="#008000">param</font> is any 32-bit value you want to pass to the thread, and <font color="#008000">priority</font> is the thread's priority,
which is represented by a number of predefined constants. Those constants and their descriptions are shown in Table 27.1.</P>
<P><I>Table 27.1—Thread Priority Constants</I></P>
<TABLE BORDER>
<TR>
<TD>
<P><B>Constant</B></P>
<TD>
<P><B>Description</B></P>
<TR>
<TD>
<pre><font color="#008000">THREAD_PRIORITY_ABOVE_NORMAL</font></pre>
<TD>
<P>Sets a priority one point higher than normal.</P>
<TR>
<TD>
<pre><font color="#008000">THREAD_PRIORITY_BELOW_NORMAL</font></pre>
<TD>
<P>Sets a priority one point lower than normal.</P>
<TR>
<TD>
<pre><font color="#008000">THREAD_PRIORITY_HIGHEST</font></pre>
<TD>
<P>Sets a priority two points above normal.</P>
<TR>
<TD>
<pre><font color="#008000">THREAD_PRIORITY_IDLE</font></pre>
<TD>
<P>Sets a base priority of 1. For a <font color="#008000">REALTIME_PRIORITY_CLASS</font> process, sets a priority of 16.</P>
<TR>
<TD>
<pre><font color="#008000">THREAD_PRIORITY_LOWEST</font></pre>
<TD>
<P>Sets a priority two points below normal.</P>
<TR>
<TD>
<pre><font color="#008000">THREAD_PRIORITY_NORMAL</font></pre>
<TD>
<P>Sets normal priority.</P>
<TR>
<TD>
<pre><font color="#008000">THREAD_PRIORITY_TIME_CRITICAL</font></pre>
<TD>
<P>Sets a base priority of 15. For a <font color="#008000">REALTIME_PRIORITY_CLASS</font> process, sets a priority of 30.</P></TABLE>
<blockquote><p><img src="note.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/note.gif">
<P>A thread's priority determines how often the thread takes control of the system relative to the other running threads. Generally, the higher the priority, the more running time the thread gets, which is why the value of <font
color="#008000">THREAD_PRIORITY_TIME_CRITICAL</font> is so high.</P>
<p><img src="bottom.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/bottom.gif"></blockquote>
<P>In order to see a simple thread in action, build the Thread application as detailed in the following steps.</P>
<ol>
<li><P> Start a new AppWizard project workspace called Thread, as shown in Figure 27.1.</P>
</ol>
<A HREF="BBfig01.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch27/BBfig01.gif"><b>Fig. 27.1</b></A>
<P><I>Start an AppWizard project workspace called Thread.</I></P>
<ol start=2>
<li><P> Give the new project the following settings in the AppWizard dialog boxes. The New Project Information dialog box should then look like Figure 27.2.</P>
<P>Step 1: Single document</P>
<P>Step 2: Default settings</P>
<P>Step 3: Default settings</P>
<P>Step 4: Turn off all options</P>
<P>Step 5: Default settings</P>
<P>Step 6: Default settings</P>
</ol>
<A HREF="BBfig02.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch27/BBfig02.gif"><b>Fig. 27.2</b></A>
<P><I>These are the AppWizard settings for the Thread project.</I></P>
<ol start=3>
<li><P> Use the resource editor to add a <U>T</U>hread menu to the application's <font color="#008000">IDR_MAINFRAME</font> menu. Give the menu one command called <U>S</U>tart Thread with a command ID of <font color="#008000">ID_STARTTHREAD</font>, and
enter a sensible prompt and ToolTip, as shown in Figure 27.3.</P>
</ol>
<A HREF="BBfig03.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch27/BBfig03.gif"><b>Fig. 27.3</b></A>
<P><I>Add a </I><I><U>T</U></I><I>hread menu with a </I><I><U>S</U></I><I>tart Thread command.</I></P>
<ol start=4>
<li><P> Use ClassWizard to associate the <font color="#008000">ID_STARTTHREAD</font> command with the <font color="#008000">OnStartthread()</font> message-response function, as shown in Figure 27.4. Make sure that you have <font
color="#008000">CThreadView</font> selected in the Class <U>N</U>ame box before you add the function.</P>
</ol>
<A HREF="BBfig04.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch27/BBfig04.gif"><b>Fig. 27.4</b></A>
<P><I>Add the </I><I>OnStartthread()</I><I> message-response function to the view class.</I></P>
<ol start=5>
<li><P> Click the <U>E</U>dit Code button and then add the following lines to the new <font color="#008000">OnStartthread()</font> function, replacing the <font color="#008000">TODO: Add your </font><font color="#008000">command handler code here</font>
comment:</P>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -