搜索
熱搜: 活動 交友 discuz
查看: 2906|回復: 0
打印 上一主題 下一主題

[教學] C++多執行緒(八)

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-8-14 04:45:25 | 只看該作者 回帖獎勵 |正序瀏覽 |閱讀模式

  1. 一 Event
  2.      在所有的內核對像中,事件內核對象是個最基本的對象。它們包含一個使用計數(與所有內核對像一樣),一個用於指明該事件是個自動重置的事件還是一個人工重置的事件的布爾值,另一個用於指明該事件處於已通知狀態還是未通知狀態的布爾值。
  3.         事件能夠通知一個操作已經完成。有兩種不同類型的事件對象。一種是人工重置的事件,另一種是自動重置的事件。當人工重置的事件得到通知時,等待該事件的所有執行緒均變為可調度執行緒。當一個自動重置的事件得到通知時,等待該事件的執行緒中只有一個執行緒變為可調度執行緒。
  4.        當一個執行緒執行初始化操作,然後通知另一個執行緒執行剩餘的操作時,事件使用得最多。事件初始化為未通知狀態,然後,當該執行緒完成它的初始化操作後,它就將事件設置為已通知狀態。這時,一直在等待該事件的另一個執行緒發現該事件已經得到通知,因此它就變成可調度執行緒。

  5.          Microsoft為自動重置的事件定義了應該成功等待的副作用規則,即當執行緒成功地等待到該對像時,自動重置的事件就會自動重置到未通知狀態。這就是自動重置的事件如何獲得它們的名字的方法。通常沒有必要為自動重置的事件調用ResetEvent函數,因為系統會自動對事件進行重置。但是,Microsoft沒有為人工重置的事件定義成功等待的副作用,所以需要調用ResetEvent()。

  6. 二 Event API

  7. [tr]            Event function            Description        [/tr]         CreateEvent Creates or opens a named or unnamed event object.
  8.          CreateEventEx Creates or opens a named or unnamed event object and returns a handle to the object.
  9.          OpenEvent Opens an existing named event object.
  10.          PulseEvent Setsthe specified event object to the signaled state and then resets it tothe nonsignaled state after releasing the appropriate number of waitingthreads.
  11.          ResetEvent Sets the specified event object to the nonsignaled state.
  12.          SetEvent Sets the specified event object to the signaled state.


  13. 三 代碼實例

  14. 1)使用手動的Event:當檔案讀入記憶體的時候,WordCount, SpellCheck,GrammarCheck可以同時進行,這裡使用Event,當檔案一讀入記憶體就通知WordCount,SpellCheck和GrammarCheck執行緒開始執行。
  15. #include <windows.h>
  16. #include <process.h>
  17. #include <stdio.h>

  18. // a global handle to event.
  19. HANDLE g_hEvent;

  20. void OpenFileAndReadContentsIntoMemory();
  21. unsigned __stdcall WordCount(void
  22. *pvParam);
  23. unsigned __stdcall SpellCheck(void
  24. *pvParam);
  25. unsigned __stdcall GrammarCheck(void
  26. *pvParam);

  27. int main()
  28. {
  29.    //Create the manual-reset、nonsignaled event.
  30.    g_hEvent = CreateEvent(NULL、TRUE、FALSE、NULL);

  31.    //Spawn 3 new threads.
  32.    HANDLE hThread[3];
  33.    unsigned dwThreadID[3];
  34.    hThread[0] = (HANDLE)_beginthreadex(NULL、0、WordCount、NULL、0、&dwThreadID[0]);
  35.    hThread[1] = (HANDLE)_beginthreadex(NULL、0、SpellCheck、NULL、0、&dwThreadID[1]);
  36.    hThread[2] = (HANDLE)_beginthreadex(NULL、0、GrammarCheck、NULL、0、&dwThreadID[2]);

  37.    OpenFileAndReadContentsIntoMemory();

  38.    //Allow all 3 threads to access the memory.
  39.    SetEvent(g_hEvent);

  40.    printf("main thread exit\n");
  41.    return
  42. 1;
  43. }
  44. void OpenFileAndReadContentsIntoMemory()
  45. {
  46.   printf("Open File and Read contents into memory\n");
  47. }
  48. unsigned __stdcall WordCount(void
  49. *pvParam)
  50. {
  51.    //Wait until the file's data is in memory.
  52.    WaitForSingleObject(g_hEvent、INFINITE);

  53.    //Access the memory block.
  54.    printf("0:word count\n");
  55.    return(0);
  56. }
  57. unsigned __stdcall SpellCheck(void
  58. *pvParam)
  59. {
  60.    //Wait until the file's data is in memory.
  61.    WaitForSingleObject(g_hEvent、INFINITE);

  62.    //Access the memory block.
  63.    printf("1:Spell check\n");
  64.    return(0);
  65. }
  66. unsigned __stdcall GrammarCheck(void
  67. *pvParam)
  68. {
  69.    //Wait until the file's data is in memory.
  70.    WaitForSingleObject(g_hEvent、INFINITE);

  71.    //Access the memory block.
  72.    printf("2:Grammar check\n");
  73.    return(0);
  74. }

  75. 2)修改上面的代碼,使用自動Event,則必須在3個子執行緒中增加SetEvent()。且要想讓3個執行緒都執行完,必須的增加Waitfor()函數。

  76. #include <windows.h>
  77. #include <process.h>
  78. #include <stdio.h>

  79. // a global handle to event.
  80. HANDLE g_hEvent;

  81. void OpenFileAndReadContentsIntoMemory();
  82. unsigned __stdcall WordCount(void
  83. *pvParam);
  84. unsigned __stdcall SpellCheck(void
  85. *pvParam);
  86. unsigned __stdcall GrammarCheck(void
  87. *pvParam);

  88. int main()
  89. {
  90.    //Create the AUTO-reset、nonsignaled event.
  91.    g_hEvent = CreateEvent(NULL、FALSE、FALSE、NULL);

  92.    //Spawn 3 new threads.
  93.    HANDLE hThread[3];
  94.    unsigned dwThreadID[3];
  95.    hThread[0] = (HANDLE)_beginthreadex(NULL、0、WordCount、NULL、0、&dwThreadID[0]);
  96.    hThread[1] = (HANDLE)_beginthreadex(NULL、0、SpellCheck、NULL、0、&dwThreadID[1]);
  97.    hThread[2] = (HANDLE)_beginthreadex(NULL、0、GrammarCheck、NULL、0、&dwThreadID[2]);

  98.    OpenFileAndReadContentsIntoMemory();

  99.    //Allow all 3 threads to access the memory.
  100.    SetEvent(g_hEvent);

  101.     //wait for child threads to exit
  102.     DWORD dwCThd = WaitForMultipleObjects (3、//count of objects
  103.                                         hThread、//thread handle
  104.                                         TRUE、//wait for all
  105.                                         INFINITE); //time out interval

  106. if(dwCThd != WAIT_OBJECT_0)
  107.     {
  108.         printf("error\n");
  109.         exit(-1);
  110.     }

  111.     //close handles
  112.     CloseHandle (g_hEvent);
  113.     //close child thread handles

  114. for (int i=0; i<3; i++)
  115.         CloseHandle (hThread);

  116.    printf("main thread exit\n");
  117.    return
  118. 1;
  119. }
  120. void OpenFileAndReadContentsIntoMemory()
  121. {
  122.   printf("Open File and Read contents into memory\n");
  123. }
  124. unsigned __stdcall WordCount(void
  125. *pvParam)
  126. {
  127.    //Wait until the file's data is in memory.
  128.    WaitForSingleObject(g_hEvent、INFINITE);

  129.    //Access the memory block.
  130.    printf("0:word count\n");
  131.    SetEvent(g_hEvent);

  132.    return(0);
  133. }
  134. unsigned __stdcall SpellCheck(void
  135. *pvParam)
  136. {
  137.    //Wait until the file's data is in memory.
  138.    WaitForSingleObject(g_hEvent、INFINITE);

  139.    //Access the memory block.
  140.    printf("1:Spell check\n");
  141.    SetEvent(g_hEvent);

  142.    return(0);
  143. }
  144. unsigned __stdcall GrammarCheck(void
  145. *pvParam)
  146. {
  147.    //Wait until the file's data is in memory.
  148.    WaitForSingleObject(g_hEvent、INFINITE);

  149.    //Access the memory block.
  150.    printf("2:Grammar check\n");

  151.    SetEvent(g_hEvent);

  152.    return(0);
  153. }

  154. 四 參考
  155. windows核心編程

複製代碼
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2026-6-4 11:32 , Processed in 0.110699 second(s), 17 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表