亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频

? 歡迎來(lái)到蟲(chóng)蟲(chóng)下載站! | ?? 資源下載 ?? 資源專輯 ?? 關(guān)于我們
? 蟲(chóng)蟲(chóng)下載站

?? ch27.htm

?? Learning language of Visual C++6
?? HTM
?? 第 1 頁(yè) / 共 4 頁(yè)
字號(hào):


<BLOCKQUOTE>
	<PRE>    threadStart.SetEvent();</PRE>

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DT></DT>
	<DD><B>6. </B>Replace the code in the OnStopthread() function with the following
	line:
	<P>
</DL>



<BLOCKQUOTE>
	<PRE>    threadEnd.SetEvent();</PRE>

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DT></DT>
	<DD><B>7.</B> Use ClassWizard to add an OnCreate() function that handles the WM_CREATE
	message, as shown in Figure 27.8. Make sure that you have <B>CThreadView</B> selected
	in the Class Name box before you add the function.
	<P>
</DL>

<P><A HREF="javascript:popUp('27uvc08.gif')"><B>FIG. 27.8</B></A><B> </B><I>Use ClassWizard
to add the OnCreate() function.</I></P>

<DL>
	<DT><I></I></DT>
	<DD><B>8. </B>Add the following lines to the OnCreate() function, replacing the TODO:
	Add your specialized creation code here comment:
	<P>
</DL>

<PRE>    HWND hWnd = GetSafeHwnd();
    AfxBeginThread(ThreadProc, hWnd);
</PRE>
<P>Again, this new version of the program seems to run just like the preceding version.
However, the program is now using both event objects and user-defined Windows messages
to communicate between the main program and the thread. No more messing with clunky
global variables.</P>
<P>One big difference from previous versions of the program is that the secondary
thread is begun in the OnCreate() function, which is called when the application
first runs and creates the view. However, because the first line of the thread function
is the call to WaitForSingleObject(), the thread immediately suspends execution and
waits for the threadStart event to be signaled.</P>
<P>When the threadStart event object is signaled, the thread is free to display the
message box and then enter its while loop, where it polls the threadEnd event object.
The while loop continues to execute until threadEnd is signaled, at which time the
thread sends the WM_THREADENDED message to the main program and exits. Because the
thread is started in OnCreate(), after the thread ends, it can't be restarted.</P>
<P>
<H2><A NAME="Heading6"></A>Using Thread Synchronization</H2>
<P>Using multiple threads can lead to some interesting problems. For example, how
do you prevent two threads from accessing the same data at the same time? What if,
for example, one thread is in the middle of trying to update a data set when another
thread tries to read that data? The second thread will almost certainly read corrupted
data because only some of the data set will have been updated.</P>
<P>Trying to keep threads working together properly is called <I>thread synchronization</I>.
Event objects, about which you just learned, are a form of thread synchronization.
In this section, you'll learn about <I>critical sections, mutexes,</I> and <I>semaphores</I>--thread
synchronization objects that make your thread programming even safer.</P>
<P>
<H3><A NAME="Heading7"></A>Using Critical Sections</H3>
<P>Critical sections are an easy way to ensure that only one thread at a time can
access a data set. When you use a critical section, you give your threads an object
that they have to share. Whichever thread possesses the critical-section object has
access to the guarded data. Other threads have to wait until the first thread releases
the critical section, after which another thread can grab the critical section to
access the data in turn.</P>
<P>Because the guarded data is represented by a single critical-section object and
because only one thread can own the critical section at any given time, the guarded
data can never be accessed by more than a single thread at a time.</P>
<P>To create a critical-section object in an MFC program, you create an instance
of the CCriticalSection class, like this:</P>
<P>
<PRE>CCriticalSection criticalSection;
</PRE>
<P>Then, when program code is about to access the data that you want to protect,
you call the critical-section object's Lock() member function, like this:</P>
<P>
<PRE>criticalSection.Lock();
</PRE>
<P>If another thread doesn't already own the critical section, Lock() gives the object
to the calling thread. That thread can then access the guarded data, after which
it calls the critical-section object's Unlock() member function:</P>
<P>
<PRE>criticalSection.Unlock();
</PRE>
<P>Unlock() releases the ownership of the critical-section object so that another
thread can grab it and access the guarded data.</P>
<P>The best way to implement something like critical sections is to build the data
you want to protect into a thread-safe class. When you do this, you no longer have
to worry about thread synchronization in the main program; the class handles it all
for you. As an example, look at Listing 27.6, which is the header file for a thread-safe
array class.</P>
<P>
<H4>Listing 27.6&#160;&#160;COUNTARRAY.H--The CCountArray Class Header File</H4>
<PRE>#include &quot;afxmt.h&quot;
class CCountArray
{
private:
    int array[10];
    CCriticalSection criticalSection;
public:
    CCountArray() {};
    ~CCountArray() {};
    void SetArray(int value);
    void GetArray(int dstArray[10]);
</PRE>
<PRE>};
</PRE>
<P>The header file starts by including the MFC header file, afxmt.h, which gives
the program access to the CCriticalSection class. Within the CCountArray class declaration,
the file declares a 10-element integer array, which is the data that the critical
section will guard, and declares the critical-section object, here called criticalSection.
The CCountArray class's public member functions include the usual constructor and
destructor, as well as functions for setting and reading the array. These latter
two member functions must deal with the critical-section object because these functions
access the array.</P>
<P>Listing 27.7 is the CCountArray class's implementation file. Notice that, in each
member function, the class takes care of locking and unlocking the critical-section
object. This means that any thread can call these member functions without worrying
about thread synchronization. For example, if thread 1 calls SetArray(), the first
thing SetArray() does is call criticalSection.Lock(), which gives the critical-section
object to thread 1. The complete for loop then executes, without any fear of being
interrupted by another thread. If thread 2 calls SetArray() or GetArray(), the call
to criticalSection.Lock() suspends thread 2 until thread 1 releases the critical-section
object, which it does when SetArray() finishes the for loop and executes the criticalSection.Unlock()
line. Then the system wakes up thread 2 and gives it the critical-section object.
In this way, all threads have to wait politely for their chance to access the guarded
data.</P>
<P>
<H4>Listing 27.7&#160;&#160;COUNTARRAY.CPP--The CCountArray Class Implementation
<BR>
File</H4>
<PRE>#include &quot;stdafx.h&quot;
#include &quot;CountArray.h&quot;
void CCountArray::SetArray(int value)
{
    criticalSection.Lock();
    for (int x=0; x&lt;10; ++x)
        array[x] = value;
    criticalSection.Unlock();
}
void CCountArray::GetArray(int dstArray[10])
{
    criticalSection.Lock();
    for (int x=0; x&lt;10; ++x)
        dstArray[x] = array[x];
    criticalSection.Unlock();
</PRE>
<PRE>}
</PRE>
<P>Now that you've had a chance to see what a thread-safe class looks like, it's
time to put the class to work. Perform the following steps, which modify the Thread
application to test the CCountArray class:</P>

<DL>
	<DT></DT>
	<DD><B>1.</B> Use the File, New command to add a new C++ header file called <B>CountArray.h</B>
	to the project, as shown in Figure 27.9. Enter the code from Listing 27.6.
	<P>
	<DT></DT>
	<DD><B>2.</B> Again choose File, New, and create a new C++ source file called <B>CountArray.cpp</B>
	in this project. Enter the code from Listing 27.7.
	<P>
	<DT></DT>
	<DD><B>3. </B>Switch to ThreadView.cpp and add the following line near the top of
	the file, after the line #include &quot;afxmt.h&quot;, which you placed there previously:
</DL>



<BLOCKQUOTE>
	<PRE>#include &quot;CountArray.h&quot;</PRE>

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DT></DT>
	<DD><B>4. </B>Add the following line near the top of the file, after the CEvent threadEnd
	line you placed there previously:
</DL>



<BLOCKQUOTE>
	<PRE>CCountArray countArray;</PRE>

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DT></DT>
	<DD><B>5. </B>Delete the CEvent threadStart and CEvent threadEnd lines from the file.
	<P>
</DL>

<P><A HREF="javascript:popUp('27uvc09.gif')"><B>FIG. 27.9</B></A><B> </B><I>Add CountArray.h
to the Thread project.</I></P>

<DL>
	<DT><I></I></DT>
	<DD><B>6. </B>Delete the lines ON_MESSAGE(WM_THREADENDED, OnThreadended), ON_COMMAND(ID_STOPTHREAD,
	OnStopthread), and ON_WM_CREATE() from the message map.
	<P>
	<DT></DT>
	<DD><B>7. </B>Replace the ThreadProc() function with the thread functions shown in
	Listing 27.8.
	<P>
</DL>

<H4>Listing 27.8&#160;&#160;WriteThreadProc() and ReadThreadProc()</H4>
<PRE>UINT WriteThreadProc(LPVOID param)
{
    for(int x=0; x&lt;10; ++x)
    {
        countArray.SetArray(x);
        ::Sleep(1000);
    }
    return 0;
}
UINT ReadThreadProc(LPVOID param)
{
    int array[10];
    for (int x=0; x&lt;20; ++x)
    {
        countArray.GetArray(array);
        char str[50];
        str[0] = 0;
        for (int i=0; i&lt;10; ++i)
        {
            int len = strlen(str);
            wsprintf(&amp;str[len], &quot;%d &quot;, array[i]);
        }
        ::MessageBox((HWND)param, str, &quot;Read Thread&quot;, MB_OK);
    }
    return 0;
</PRE>
<PRE>}
</PRE>

<DL>
	<DT></DT>
	<DD><B>8. </B>Replace all the code in the OnStartthread() function with the following
	lines:
	<P>
</DL>



<BLOCKQUOTE>
	<PRE>    HWND hWnd = GetSafeHwnd();
    AfxBeginThread(WriteThreadProc, hWnd);
    AfxBeginThread(ReadThreadProc, hWnd);</PRE>

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DT></DT>
	<DD><B>9. </B>Delete the OnStopthread(), OnThreadended, and OnCreate() functions
	from the file.
	<P>
	<DT></DT>
	<DD><B>10. </B>Switch to the ThreadView.h file and delete the line const WM_THREADENDED
	= WM_USER + 100 from the listing.
	<P>
	<DT></DT>
	<DD><B>11. </B>Also, in ThreadView.h, delete the lines afx_msg LONG OnThreadended(WPARAM
	wParam, LPARAM lParam), afx_msg void OnStopthread(), and afx_msg int OnCreate(LPCREATESTRUCT
	lpCreateStruct) from the message map.
	<P>
	<DT></DT>
	<DD><B>12. </B>Using the resource editor, remove the Stop Thread command from the
	Thread menu.
	<P>
</DL>

<P>Now build and run the new version of the Thread application. When you do, the
main window appears. Select the Thread, Start Thread command to get things hopping.
The first thing you'll see is a message box (see Figure 27.10) displaying the current
values in the guarded array. Each time you dismiss the message box, it reappears
with the array's new contents. The message box will reappear 20 times. The values
listed in the message box depend on how often you dismiss the message box. The first
thread is writing new values into the array once a second, even as you're viewing
the array's contents in the second thread.</P>
<P><A HREF="javascript:popUp('27uvc10.gif')"><B>FIG. 27.10</B></A><B> </B><I>This
message box displays the current contents of the guarded array.</I></P>

<P>The important thing to notice is that at no time does the second thread interrupt
when the first thread is changing the values in the array. You can tell that this
is true because the array always contains 10 identical values. If the first thread
were interrupted as it modified the array, the 10 values in the array would not be
identical, as shown in Figure 27.11.</P>
<P>If you examine the source code carefully, you'll see that the first thread, named
WriteThreadProc(), is calling the array class's SetArray() member function 10 times
within a for loop. Each time through the loop, SetArray() gives the thread the critical-section
object, changes the array contents to the passed number, and then takes the critical-section
object away again. Note the call to the Sleep() function, which suspends the thread
for the number of milliseconds given as the function's single argument.</P>
<P><A HREF="javascript:popUp('27uvc11.gif')"><B>FIG. 27.11</B></A><B> </B><I>Without
thread synchronization, you might see something like this in the message box.</I></P>

<P>The second thread, named ReadThreadProc(), is also trying to access the same critical-section
object to construct a display string of the values contained in the array. However,
if WriteThreadProc() is currently trying to fill the array with new values, ReadThreadProc()

?? 快捷鍵說(shuō)明

復(fù)制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號(hào) Ctrl + =
減小字號(hào) Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
亚洲免费视频成人| 国产成人精品一区二| 免费欧美高清视频| 欧美videos中文字幕| 欧美精品乱码久久久久久按摩| 日本韩国一区二区| av激情亚洲男人天堂| 国产91露脸合集magnet| 韩国欧美国产1区| 久草这里只有精品视频| 蜜桃91丨九色丨蝌蚪91桃色| 精品亚洲porn| 国产真实乱对白精彩久久| 国产凹凸在线观看一区二区| 亚洲美女精品一区| 亚洲一区二区偷拍精品| 天堂av在线一区| 国产精品自在在线| 在线亚洲高清视频| 7777女厕盗摄久久久| www国产精品av| 亚洲柠檬福利资源导航| 男男成人高潮片免费网站| 国产老肥熟一区二区三区| 91日韩精品一区| 日韩欧美一区在线观看| 自拍偷拍国产精品| 日本午夜一本久久久综合| 国产91精品一区二区| 欧美在线观看一二区| 久久久久久97三级| 一级中文字幕一区二区| 国内欧美视频一区二区| 91在线视频在线| 日韩一区二区在线免费观看| 国产精品美女久久久久久2018| 亚洲成人免费看| 国产成人在线视频网址| 欧美一区三区四区| 亚洲色图在线视频| 美女视频黄久久| 成人午夜av在线| 日韩欧美色综合| 一区二区三区四区在线免费观看 | 久久久久国产成人精品亚洲午夜| 亚洲情趣在线观看| 成人小视频免费观看| 欧美一区二区网站| 亚洲天天做日日做天天谢日日欢| 狠狠色丁香婷婷综合| 欧美日韩国产另类不卡| 亚洲女子a中天字幕| 经典三级在线一区| 91精品欧美福利在线观看| 亚洲永久免费视频| 99久久精品免费看国产免费软件| 精品国产髙清在线看国产毛片| 亚洲图片欧美一区| 一本久道久久综合中文字幕| 国产欧美日韩麻豆91| 久久精品国产99| 欧美亚洲图片小说| 欧美激情在线观看视频免费| 伦理电影国产精品| 夜夜嗨av一区二区三区网页| 韩国精品在线观看| 久久综合九色综合97婷婷| 日本成人在线不卡视频| 777精品伊人久久久久大香线蕉| 亚洲国产精品视频| 欧美日韩精品欧美日韩精品一| 亚洲色欲色欲www在线观看| 国产jizzjizz一区二区| 日本不卡视频在线观看| 色8久久精品久久久久久蜜 | 一区二区三区在线视频免费| 成人综合婷婷国产精品久久 | 欧美精品一二三| 午夜国产精品一区| 欧洲激情一区二区| 亚洲美腿欧美偷拍| 91首页免费视频| 1000精品久久久久久久久| 成人性色生活片免费看爆迷你毛片| 久久九九久久九九| 成人妖精视频yjsp地址| 国产精品的网站| 91片在线免费观看| 一区二区三区中文字幕| 欧美日韩一本到| 婷婷综合另类小说色区| 日韩亚洲欧美一区二区三区| 日韩中文欧美在线| 精品国产乱码久久久久久夜甘婷婷| 久久99精品网久久| 欧美精品一区二区不卡| 国产激情偷乱视频一区二区三区| 精品免费国产二区三区| 国产在线不卡一区| 国产香蕉久久精品综合网| 国产毛片精品一区| 亚洲国产高清在线| 在线观看亚洲精品| 亚洲视频你懂的| 欧美日韩精品一区二区三区蜜桃| 天天av天天翘天天综合网色鬼国产| 欧美日韩日日骚| 黄色成人免费在线| 欧美国产精品专区| 91丨porny丨蝌蚪视频| 亚洲电影激情视频网站| 久久久综合精品| 色婷婷亚洲综合| 捆绑调教一区二区三区| 国产精品久久久久天堂| 欧美日韩精品欧美日韩精品一| 国产一区二区免费看| 一区二区三区免费看视频| 亚洲精品一区二区精华| 一本一本大道香蕉久在线精品| 日本美女视频一区二区| 中文字幕精品—区二区四季| 欧美三级在线看| 国产成人精品免费| 午夜成人在线视频| 久久精品国产秦先生| 蜜臀国产一区二区三区在线播放 | 亚洲成人av免费| 亚洲成a人在线观看| 蜜桃av一区二区三区| 毛片一区二区三区| 顶级嫩模精品视频在线看| 91亚洲精品乱码久久久久久蜜桃| 91尤物视频在线观看| 欧美性感一区二区三区| 亚洲精品一区二区三区四区高清| 国产精品福利在线播放| 日韩亚洲国产中文字幕欧美| 大白屁股一区二区视频| 日日摸夜夜添夜夜添精品视频 | 久久精品国产精品亚洲综合| 亚洲视频在线观看三级| 久久久久久久电影| 欧美精品久久一区二区三区| 99re6这里只有精品视频在线观看| 蜜桃视频第一区免费观看| 亚洲免费av观看| 中文字幕的久久| 久久综合久久鬼色中文字| 欧美日韩国产综合视频在线观看| 成人黄色777网| 国产乱对白刺激视频不卡| 日本欧美肥老太交大片| 一区二区三区成人在线视频| 中文字幕一区av| 久久精品视频在线看| 欧美变态凌虐bdsm| 欧美高清视频一二三区| 欧美午夜精品久久久久久孕妇| hitomi一区二区三区精品| 精品一区二区三区影院在线午夜| 首页国产欧美日韩丝袜| 亚洲一本大道在线| 一区二区三区四区国产精品| 亚洲欧洲日韩在线| 国产精品久久久久久久久免费樱桃 | 国产亚洲一本大道中文在线| 欧美久久久久久久久| 欧美日韩国产在线观看| 91久久人澡人人添人人爽欧美| 99久久久精品免费观看国产蜜| 国产a视频精品免费观看| 国产美女视频一区| 国产一区二区在线影院| 国产最新精品精品你懂的| 精品影视av免费| 国内一区二区在线| 国产一区二区精品久久99| 久久不见久久见免费视频1| 麻豆精品一二三| 开心九九激情九九欧美日韩精美视频电影 | 国产成人免费视频网站| 国产乱人伦偷精品视频免下载| 久久国产乱子精品免费女| 久久精品国产免费看久久精品| 久久国产夜色精品鲁鲁99| 精品一区二区三区免费毛片爱 | 日本一区二区三区在线不卡| 日韩一区二区三| 国产资源在线一区| 午夜影院久久久| 美女视频黄 久久| 久久亚洲一区二区三区四区| 不卡av在线网| 天堂精品中文字幕在线| 久久嫩草精品久久久精品| 成人av一区二区三区| 一区二区在线观看视频在线观看| 欧美日本在线播放| 国产一区91精品张津瑜|