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

? 歡迎來到蟲蟲下載站! | ?? 資源下載 ?? 資源專輯 ?? 關于我們
? 蟲蟲下載站

?? ch27.htm

?? Learning language of Visual C++6
?? HTM
?? 第 1 頁 / 共 4 頁
字號:
has to wait. The inverse is also true. That is, WriteThreadProc() can't access the
guarded data until it can regain ownership of the critical section from ReadThreadProc().</P>
<P>If you really want to prove that the critical-section object is working, remove
the criticalSection.Unlock() line from the end of the CCountArray class's SetArray()
member function. Then compile and run the program. This time when you start the threads,
no message box appears. Why? Because WriteThreadProc() takes the critical-section
object and never lets it go, which forces the system to suspend ReadThreadProc()
forever (or at least until you exit the program).</P>
<P>
<H3><A NAME="Heading8"></A>Using Mutexes</H3>
<P>Mutexes are a lot like critical sections but a little more complicated because
they enable safe sharing of resources, not only between threads in the same application
but also between threads of different applications. Although synchronizing threads
of different applications is beyond the scope of this chapter, you can get a little
experience with mutexes by using them in place of critical sections.</P>
<P>Listing 27.9 is the CCountArray2 class's header file. Except for the new classname
and the mutex object, this header file is identical to the original CountArray.h.
Listing 27.10 is the modified class's implementation file. As you can see, the member
functions look a lot different when they are using mutexes instead of critical sections,
even though both objects provide essentially the same type of services.</P>
<P>
<H4>Listing 27.9&#160;&#160;CCOUNTARRAY2.H--The CCountArray2 Class Header File</H4>
<PRE>#include &quot;afxmt.h&quot;
class CCountArray2
{
private:
    int array[10];
    CMutex mutex;
public:
    CCountArray2() {};
    ~CCountArray2() {};
    void SetArray(int value);
    void GetArray(int dstArray[10]);
</PRE>
<PRE>};
</PRE>
<H4>Listing 27.10&#160;&#160;COUNTARRAY2.CPP--The CCountArray2 Class <BR>
Implementation File</H4>
<PRE>#include &quot;stdafx.h&quot;
#include &quot;CountArray2.h&quot;
void CCountArray2::SetArray(int value)
{
    CSingleLock singleLock(&amp;mutex);
    singleLock.Lock();
    for (int x=0; x&lt;10; ++x)
        array[x] = value;
}
void CCountArray2::GetArray(int dstArray[10])
{
    CSingleLock singleLock(&amp;mutex);
    singleLock.Lock();
    for (int x=0; x&lt;10; ++x)
        dstArray[x] = array[x];
</PRE>
<PRE>}
</PRE>
<P>To access a mutex object, you must create a CSingleLock or CMultiLock object,
which performs the actual access control. The CCountArray2 class uses CSingleLock
objects because this class is dealing with only a single mutex. When the code is
about to manipulate guarded resources (in this case, the array), you create a CSingleLock
object, like this:</P>
<P>
<PRE>CSingleLock singleLock(&amp;mutex);
</PRE>
<P>The constructor's argument is a pointer to the thread-synchronization object that
you want to control. Then, to gain access to the mutex, you call the CSingleLock
object's Lock() member function:</P>
<P>
<PRE>singleLock.Lock();
</PRE>
<P>If the mutex is unowned, the calling thread becomes the owner. If another thread
already owns the mutex, the system suspends the calling thread until the mutex is
released, at which time the waiting thread is awakened and takes control of the mutex.</P>
<P>To release the mutex, you call the CSingleLock object's Unlock() member function.
However, if you create your CSingleLock object on the stack (rather than on the heap,
using the new operator) as shown in Listing 27.10, you don't have to call Unlock()
at all. When the function exits, the object goes out of scope, which causes its destructor
to execute. The destructor automatically unlocks the object for you.</P>
<P>To try out the new CCountArray2 class in the Thread application, add new CountArray2.h
and CountArray2.cpp files to the Thread project and then delete the original CountArray.h
and CountArray.cpp files. Finally, in ThreadView.cpp, change all references to CCountArray
to CCountArray2. Because all the thread synchronization is handled in the CCountArray2
class, no further changes are necessary to use mutexes instead of critical sections.</P>
<P>
<H3><A NAME="Heading9"></A>Using Semaphores</H3>
<P>Although semaphores are used like critical sections and mutexes in an MFC program,
they serve a slightly different function. Rather than enable only one thread to access
a resource at a time, semaphores enable multiple threads to access a resource, but
only to a point. That is, semaphores enable a maximum number of threads to access
a resource simultaneously.</P>
<P>When you create the semaphore, you tell it how many threads should be allowed
simultaneous access to the resource. Then, each time a thread grabs the resource,
the semaphore decrements its internal counter. When the counter reaches 0, no further
threads are allowed access to the guarded resource until another thread releases
the resource, which increments the semaphore's counter.</P>
<P>You create a semaphore by supplying the initial count and the maximum count, like
this:</P>
<P>
<PRE>CSemaphore Semaphore(2, 2);
</PRE>
<P>Because in this section you'll be using a semaphore to create a thread-safe class,
it's more convenient to declare a CSemaphore pointer as a data member of the class
and then create the CSemaphore object dynamically in the class's constructor, like
this:</P>
<P>
<PRE>semaphore = new CSemaphore(2, 2);
</PRE>
<P>You should do this because you have to initialize a data member in the constructor
rather than at the time you declare it. With the critical-section and mutex objects,
you didn't have to supply arguments to the class's constructors, so you were able
to create the object at the same time you declared it.</P>
<P>After you have created the semaphore object, it's ready to start counting resource
access. To implement the counting process, you first create a CSingleLock object
(or CMultiLock, if you're dealing with multiple thread-synchronization objects),
giving it a pointer to the semaphore you want to use, like this:</P>
<P>
<PRE>CSingleLock singleLock(semaphore);
</PRE>
<P>Then, to decrement the semaphore's count, you call the CSingleLock object's Lock()
member function:</P>
<P>
<PRE>singleLock.Lock();
</PRE>
<P>At this point, the semaphore object has decremented its internal counter. This
new count remains in effect until the semaphore object is released, which you can
do explicitly by calling the object's Unlock() member function:</P>
<P>
<PRE>singleLock.Unlock();
</PRE>
<P>Alternatively, if you've created the CSingleLock object locally on the stack,
you can just let the object go out of scope, which not only automatically deletes
the object but also releases the hold on the semaphore. In other words, both calling
Unlock() and deleting the CSingleLock object increment the semaphore's counter, enabling
a waiting thread to access the guarded resource.</P>
<P>Listing 27.11 is the header file for a class called CSomeResource. CSomeResource
is a mostly useless class whose only calling is to demonstrate the use of semaphores.
The class has a single data member, which is a pointer to a CSemaphore object. The
class also has a constructor and destructor, as well as a member function called
UseResource(), which is where the semaphore will be used.</P>
<P>
<H4>Listing 27.11&#160;&#160;SOMERESOURCE.H</H4>
<PRE>#include &quot;afxmt.h&quot;
class CSomeResource
{
private:
    CSemaphore* semaphore;
public:
    CSomeResource();
    ~CSomeResource();
    void UseResource();
</PRE>
<PRE>};
</PRE>
<P>Listing 27.12 shows the CSomeResource class's implementation file. You can see
that the CSemaphore object is constructed dynamically in the class's constructor
and deleted in the destructor. The UseResource() member function simulates accessing
a resource by attaining a count on the semaphore and then sleeping for five seconds,
after which the hold on the semaphore is released when the function exits and the
CSingleLock object goes out of scope.</P>
<P>
<H4>Listing 27.12&#160;&#160;SOMERESOURCE.CPP</H4>
<PRE>#include &quot;stdafx.h&quot;
#include &quot;SomeResource.h&quot;
CSomeResource::CSomeResource()
{
    semaphore = new CSemaphore(2, 2);
}
CSomeResource::~CSomeResource()
{
    delete semaphore;
}
void CSomeResource::UseResource()
{
    CSingleLock singleLock(semaphore);
    singleLock.Lock();
    Sleep(5000);
</PRE>
<PRE>}
</PRE>
<P>If you modify the Thread application to use the CSomeResource object, you can
watch semaphores at work. Follow these steps:</P>

<DL>
	<DT></DT>
	<DD><B>1. </B>Delete any CountArray files that are still in the project. (In FileView,
	click the file once to select it; then press Del to delete the file from the project.)
	<P>
	<DT></DT>
	<DD><B>2. </B>Create the new empty <B>SomeResource.h</B> and <B>SomeResource.cpp</B>
	files in the project.
	<P>
	<DT></DT>
	<DD><B>3. </B>Add the code from Listings 27.11 and 27.12 to these empty files.
	<P>
	<DT></DT>
	<DD><B>4. </B>Load ThreadView.cpp and replace the line #include &quot;CountArray2.h&quot;
	with the following:
	<P>
</DL>



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

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DT></DT>
	<DD><B>5. </B>Replace the line CCountArray2 countArray with the following:
	<P>
</DL>



<BLOCKQUOTE>
	<PRE>CSomeResource someResource;</PRE>

</BLOCKQUOTE>

<PRE></PRE>

<DL>
	<DT></DT>
	<DD><B>6. </B>Replace the WriteThreadProc() and ReadThreadProc() functions with the
	functions shown in Listing 27.13.
	<P>
</DL>

<H4>Listing 27.13&#160;&#160;ThreadProc1(), ThreadProc2(), and ThreadProc3()</H4>
<PRE>UINT ThreadProc1(LPVOID param)
{
    someResource.UseResource();
    ::MessageBox((HWND)param,
        &quot;Thread 1 had access.&quot;, &quot;Thread 1&quot;, MB_OK);
    return 0;
}
UINT ThreadProc2(LPVOID param)
{
    someResource.UseResource();
    ::MessageBox((HWND)param,
        &quot;Thread 2 had access.&quot;, &quot;Thread 2&quot;, MB_OK);
    return 0;
}
UINT ThreadProc3(LPVOID param)
{
    someResource.UseResource();
    ::MessageBox((HWND)param,
        &quot;Thread 3 had access.&quot;, &quot;Thread 3&quot;, MB_OK);
    return 0;
</PRE>
<PRE>}
</PRE>

<DL>
	<DT></DT>
	<DD><B>7. </B>Replace the code in the OnStartthread() function with that shown in
	Listing 27.14.
	<P>
</DL>

<H4>Listing 27.14&#160;&#160;LST27_14.TXT--New Code for the OnStartthread() Function</H4>
<PRE>    HWND hWnd = GetSafeHwnd();
    AfxBeginThread(ThreadProc1, hWnd);
    AfxBeginThread(ThreadProc2, hWnd);
</PRE>
<PRE>    AfxBeginThread(ThreadProc3, hWnd);
</PRE>
<P>Now compile and run the new version of the Thread application. When the main window
appears, select the Thread, Start Thread command. In about five seconds, two message
boxes will appear, informing you that thread 1 and thread 2 had access to the guarded
resource. About five seconds after that, a third message box will appear, telling
you that thread 3 also had access to the resource. Thread 3 took five seconds longer
because thread 1 and thread 2 grabbed control of the resource first. The semaphore
is set to allow only two simultaneous resource accesses, so thread 3 had to wait
for thread 1 or thread 2 to release its hold on the semaphore.</P>


<BLOCKQUOTE>
	<P>
<HR>
<strong>NOTE:</strong> Although the sample programs in this chapter have demonstrated using
	a single thread-synchronization object, you can have as many synchronization objects
	as you need in a single program. You can even use critical sections, mutexes, and
	semaphores all at once to protect different data sets and resources in different
	ways.&#160;
<HR>


</BLOCKQUOTE>

<P>For complex applications, threads offer the capability to maintain fast and efficient
data processing. You no longer have to wait for one part of the program to finish
its task before moving on to something else. For example, a spreadsheet application
could use one thread to update the calculations while the main thread continues accepting
entries from the user. Using threads, however, leads to some interesting problems,
not the least of which is the need to control access to shared resources. Writing
a threaded application requires thought and careful consideration of how the threads
will be used and what resources they'll access.</P>
<H1></H1>
<CENTER>
<P>
<HR>
<A HREF="../ch26/ch26.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch28/ch28.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
</P>

<P>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>


</BODY>

</HTML>

?? 快捷鍵說明

復制代碼 Ctrl + C
搜索代碼 Ctrl + F
全屏模式 F11
切換主題 Ctrl + Shift + D
顯示快捷鍵 ?
增大字號 Ctrl + =
減小字號 Ctrl + -
亚洲欧美第一页_禁久久精品乱码_粉嫩av一区二区三区免费野_久草精品视频
不卡电影免费在线播放一区| 风流少妇一区二区| 国产欧美一区二区三区网站| 99国产精品国产精品毛片| 蜜臂av日日欢夜夜爽一区| 国产亚洲欧美日韩日本| 欧洲亚洲精品在线| 精品一区二区三区免费毛片爱| 国产精品午夜在线观看| 日韩免费高清电影| 成人激情文学综合网| 精品伦理精品一区| 午夜免费久久看| 99视频有精品| 欧美国产亚洲另类动漫| 91视频观看免费| 中文字幕一区二区三区乱码在线| 国产成人久久精品77777最新版本| 宅男在线国产精品| 视频一区免费在线观看| 欧美精品一区二区蜜臀亚洲| 蜜桃视频免费观看一区| 久久久www成人免费毛片麻豆| 国产一区二三区| 日本一区二区视频在线| 色999日韩国产欧美一区二区| ㊣最新国产の精品bt伙计久久| 日韩成人免费在线| 成熟亚洲日本毛茸茸凸凹| 国产亚洲欧美一级| 97精品视频在线观看自产线路二| 成人h动漫精品| 色综合中文字幕国产 | 亚洲欧美在线另类| 日韩视频一区二区| 欧美一区二区不卡视频| 51精品视频一区二区三区| 欧美日韩国产片| 欧亚洲嫩模精品一区三区| 日本道在线观看一区二区| 91麻豆国产在线观看| 91丝袜美腿高跟国产极品老师| 成人av第一页| 一本色道久久综合亚洲精品按摩| 99久久精品一区二区| 国产精品影视网| 成人动漫视频在线| k8久久久一区二区三区| 99精品久久99久久久久| 色哟哟亚洲精品| 欧美视频一区二区在线观看| 欧美日韩国产在线播放网站| 91精品国产综合久久久久久漫画 | 本田岬高潮一区二区三区| 成人毛片在线观看| 99re成人精品视频| 欧美色窝79yyyycom| 884aa四虎影成人精品一区| 欧美精品九九99久久| 欧美一级片在线看| 久久久精品日韩欧美| 国产精品久久久久三级| 亚洲少妇30p| 日韩在线观看一区二区| 狠狠色狠狠色综合| 成人av电影免费观看| 欧美视频一区在线| 欧美mv日韩mv国产网站app| 欧美国产97人人爽人人喊| 一区二区欧美精品| 激情伊人五月天久久综合| 丰满白嫩尤物一区二区| 91丨九色丨黑人外教| 91精品国产欧美一区二区成人| 国产网红主播福利一区二区| 亚洲精品国产a| 免费成人性网站| 成人在线一区二区三区| 国产一区二区看久久| 亚洲精品在线免费观看视频| 国产精品传媒在线| 日韩国产成人精品| 春色校园综合激情亚洲| 在线综合+亚洲+欧美中文字幕| 国产亚洲精品资源在线26u| 一区二区三区影院| 极品美女销魂一区二区三区免费| 国产成人久久精品77777最新版本| 欧美性感一区二区三区| 久久久不卡网国产精品二区| 亚洲第一激情av| 国产成人免费在线视频| 欧美挠脚心视频网站| 国产精品久久久久一区二区三区| 免费欧美日韩国产三级电影| 99精品视频在线观看| 精品久久国产字幕高潮| 尤物视频一区二区| 国产盗摄精品一区二区三区在线 | 色综合久久久久综合体桃花网| 91精品国产丝袜白色高跟鞋| 中文字幕制服丝袜一区二区三区 | 国产精品白丝jk黑袜喷水| 欧美色图天堂网| 国产精品久久久久影视| 国产在线不卡一区| 7777女厕盗摄久久久| 亚洲综合色区另类av| 北条麻妃国产九九精品视频| 久久久精品影视| 久久99精品网久久| 欧美日韩高清在线播放| 亚洲日本在线a| 粉嫩13p一区二区三区| 日韩欧美卡一卡二| 日本午夜一区二区| 欧美日韩免费不卡视频一区二区三区| 国产精品久久久久久久蜜臀| 韩国v欧美v亚洲v日本v| 日韩精品一区二区三区视频播放 | 青草av.久久免费一区| 欧美在线三级电影| 亚洲欧美区自拍先锋| 国产成人免费视频网站高清观看视频| 337p亚洲精品色噜噜| 亚洲一区二区三区四区中文字幕| 99久久国产综合色|国产精品| 国产欧美精品一区aⅴ影院| 国产精品小仙女| 国产偷国产偷精品高清尤物| 国产精品影视在线| 欧美激情一区三区| 成人黄色在线网站| 国产精品国产三级国产普通话99| 国产成人亚洲综合a∨婷婷| 久久久精品免费免费| 国产精品一区二区91| 国产亚洲一二三区| 国产suv精品一区二区三区| 国产精品人成在线观看免费 | 91黄视频在线| 亚洲已满18点击进入久久| 在线观看视频一区| 亚洲国产欧美另类丝袜| 欧美人妇做爰xxxⅹ性高电影| 午夜电影久久久| 日韩欧美一级片| 国产在线精品免费av| 欧美激情一区二区| 91伊人久久大香线蕉| 亚洲免费观看视频| 欧美日韩一区二区欧美激情| 日韩精品久久理论片| 日韩欧美三级在线| 国产黄色精品视频| 日韩一区在线播放| 欧美精品乱码久久久久久按摩| 麻豆中文一区二区| 欧美激情在线一区二区| 欧美亚男人的天堂| 免费精品99久久国产综合精品| 久久免费偷拍视频| 91免费在线视频观看| 天天操天天干天天综合网| 精品日韩一区二区| 日本一区二区三级电影在线观看| 亚欧色一区w666天堂| 日韩视频免费观看高清完整版在线观看| 欧美aaa在线| 国产精品美女久久久久久| 色婷婷综合五月| 久久av中文字幕片| 国产精品白丝在线| 日韩欧美国产不卡| thepron国产精品| 日本成人在线看| 亚洲欧洲美洲综合色网| 欧美一区二区三区免费观看视频| 国产精品91xxx| 亚洲成人av电影在线| 国产日产欧美一区二区三区| 欧美在线一区二区| 国产精品888| 亚洲成人久久影院| 日本一区二区免费在线| 欧美日韩亚洲综合一区二区三区 | 免费成人av资源网| 国产精品国产三级国产aⅴ入口| 欧美日本一道本| jvid福利写真一区二区三区| 免费高清成人在线| 一区二区三区色| 久久午夜电影网| 69成人精品免费视频| 色综合色综合色综合 | 国产一区在线观看视频| 亚洲国产视频一区二区| 久久久精品影视| 日韩美女主播在线视频一区二区三区| av动漫一区二区|