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

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

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

  1.    多執行緒之等待函數
  2. 一 等待函數

  3. 1)函數列舉

  4. [tr]            Wait function            Description        [/tr]         MsgWaitForMultipleObjects Waitsuntil one or all of the specified objects are in the signaled state orthe time-out interval elapses. The objects can include input eventobjects.
  5.          MsgWaitForMultipleObjectsEx Waitsuntil one or all of the specified objects are in the signaled state、anI/O completion routine or asynchronous procedure call (APC) is queuedto the thread、or the time-out interval elapses. The array of objectscan include input event objects.
  6.          RegisterWaitForSingleObject Directs a wait thread in the thread pool to wait on the object.
  7.          SignalObjectAndWait Atomically signals one object and waits on another object.
  8.          UnregisterWait Cancels a registered wait operation.
  9.          UnregisterWaitEx Cancels a registered wait operation.
  10.          WaitForMultipleObjects Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
  11.          WaitForMultipleObjectsEx Waitsuntil one or all of the specified objects are in the signaled state、anI/O completion routine or asynchronous procedure call (APC) is queuedto the thread、or the time-out interval elapses.
  12.          WaitForSingleObject Waits until the specified object is in the signaled state or the time-out interval elapses.
  13.          WaitForSingleObjectEx Waitsuntil the specified object is in the signaled state、an I/O completionroutine or asynchronous procedure call (APC) is queued to the thread,or the time-out interval elapses.
  14.          WaitOrTimerCallback An application-defined function that serves as the starting address for a timer callback or a registered wait callback.


  15. [tr]            Waitable-timer function            Description        [/tr]         CancelWaitableTimer Sets the specified waitable timer to the inactive state.
  16.          CreateWaitableTimer Creates or opens a waitable timer object.
  17.          CreateWaitableTimerEx Creates or opens a waitable timer object and returns a handle to the object.
  18.          OpenWaitableTimer Opens an existing named waitable timer object.
  19.          SetWaitableTimer Activates the specified waitable timer.
  20.          TimerAPCProc Application-defined timer completion routine used with the SetWaitableTimer function.

  21. 2)簡單說明WaitForSingleObject

  22. DWORD WaitForSingleObject(HANDLE hObject,DWORD dwMilliseconds);
  23. 參數hObject:要等待的內核對象的句柄。
  24. 參數dwMilliseconds: 設置的等待超時的時間,以毫秒為單位。可以設置為INGINIT。     
  25.                                      順便說一下,INFINITE已經定義為0xFFFFFFFF(或-1)。當然,傳遞INFINITE有些危險。如果對像永遠不變為已
  26.                                      通知狀態,那麼調用執行緒永遠不會被喚醒,它將永遠處於死鎖狀態。
  27. 返回值:WAIT_OBJECT_0表示要等待的對象已經變為已通知的狀態。
  28.                  WAIT_TIMEOUT表示設置的時間超時。
  29.                  WAIT_FAILED表示失敗,可能是傳入的handle不正確或其他的問題。

  30. DWORD dw = WaitForSingleObject(hProcess、5000);
  31. switch(dw)
  32. {
  33.    case WAIT_OBJECT_0:
  34.       // The process terminated.

  35. break;

  36.    case WAIT_TIMEOUT:
  37.       // The process did not terminate within 5000 milliseconds.

  38. break;

  39.    case WAIT_FAILED:
  40.       // Bad call to function (invalid handle?)

  41. break;
  42. }
  43. 3)簡單說明WaitForMultipleObjects
  44. DWORD WaitForMultipleObjects(DWORD dwCount,CONST HANDLE* phObjects,BOOL fWaitAll,DWORD dwMilliseconds);
  45. 參數dwCout:需要等待的內核對象的數量。
  46. 參數phObjects:需要等待的內核對象的是數組的指針。
  47. 參數fWaitAll:表示是否需要等待所有的內核對象。
  48. 參數dwMilliseconds:設置等待超時的時間。(同上函數)
  49. 返回值:WAIT_FAILED和WAIT_TIMEOUT同上函數。
  50. 如果為fWaitAll參數傳遞TRUE,同時所有對象均變為已通知狀態,那麼返回值是WAIT_OBJECT_0。如果為fWaitAll傳遞FALSE      ,那麼一旦任何一個對像變為已通知狀態,該函數便返回。在這種情況下,你可能想要知道哪個對象變為已通知狀態。返回值是WAIT_OBJECT_0與(WAIT_OBJECT_0+dwCount- 1)之間的一個值。


  51. HANDLE h[3];
  52. h[0] = hProcess1;
  53. h[1] = hProcess2;
  54. h[2] = hProcess3;
  55. DWORD dw = WaitForMultipleObjects(3、h、FALSE、5000);
  56. switch(dw)
  57. {
  58.    case WAIT_FAILED:
  59.       // Bad call to function (invalid handle?)

  60. break;

  61.    case WAIT_TIMEOUT:
  62.       // None of the objects became signaled within 5000 milliseconds.

  63. break;

  64.    case WAIT_OBJECT_0 +
  65. 0:
  66.       // The process identified by h[0] (hProcess1) terminated.

  67. break;

  68.    case WAIT_OBJECT_0 +
  69. 1:
  70.       // The process identified by h[1] (hProcess2) terminated.

  71. break;

  72.    case WAIT_OBJECT_0 +
  73. 2:
  74.       // The process identified by h[2] (hProcess3) terminated.

  75. break;
  76. }

  77. 二 參考msdn和windows核心編程。



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

本版積分規則

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


Page Rank Check

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

手機版|中央論壇

GMT+8, 2026-6-4 12:29 , Processed in 0.033877 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

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