C++ Programmer's Cookbook

{C++ 基础} {C++ 高级} {C#界面,C++核心算法} {设计模式} {C#基础}

C++多线程(五)

 
多线程之等待函数
一 等待函数

1)函数列举

Wait function Description
MsgWaitForMultipleObjects Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses. The objects can include input event objects.
MsgWaitForMultipleObjectsEx Waits until one or all of the specified objects are in the signaled state, an I/O completion routine or asynchronous procedure call (APC) is queued to the thread, or the time-out interval elapses. The array of objects can include input event objects.
RegisterWaitForSingleObject Directs a wait thread in the thread pool to wait on the object.
SignalObjectAndWait Atomically signals one object and waits on another object.
UnregisterWait Cancels a registered wait operation.
UnregisterWaitEx Cancels a registered wait operation.
WaitForMultipleObjects Waits until one or all of the specified objects are in the signaled state or the time-out interval elapses.
WaitForMultipleObjectsEx Waits until one or all of the specified objects are in the signaled state, an I/O completion routine or asynchronous procedure call (APC) is queued to the thread, or the time-out interval elapses.
WaitForSingleObject Waits until the specified object is in the signaled state or the time-out interval elapses.
WaitForSingleObjectEx Waits until the specified object is in the signaled state, an I/O completion routine or asynchronous procedure call (APC) is queued to the thread, or the time-out interval elapses.
WaitOrTimerCallback An application-defined function that serves as the starting address for a timer callback or a registered wait callback.


Waitable-timer function Description
CancelWaitableTimer Sets the specified waitable timer to the inactive state.
CreateWaitableTimer Creates or opens a waitable timer object.
CreateWaitableTimerEx Creates or opens a waitable timer object and returns a handle to the object.
OpenWaitableTimer Opens an existing named waitable timer object.
SetWaitableTimer Activates the specified waitable timer.
TimerAPCProc Application-defined timer completion routine used with the SetWaitableTimer function.


2)简单说明WaitForSingleObject

DWORD WaitForSingleObject(HANDLE hObject,DWORD dwMilliseconds);

参数hObject:要等待的内核对象的句柄。
参数dwMilliseconds: 设置的等待超时的时间,以毫秒为单位。可以设置为INGINIT。     
                                     顺便说一下,INFINITE已经定义为0xFFFFFFFF(或-1)。当然,传递INFINITE有些危险。如果对象永远不变为已
                                     通知状态,那么调用线程永远不会被唤醒,它将永远处于死锁状态。
返回值:WAIT_OBJECT_0表示要等待的对象已经变为已通知的状态。
                 WAIT_TIMEOUT表示设置的时间超时。
                 WAIT_FAILED表示失败,可能是传入的handle不正确或其他的问题。

DWORD dw = WaitForSingleObject(hProcess, 5000);
switch(dw)
{
   
case WAIT_OBJECT_0:
      
// The process terminated.
      break;

   
case WAIT_TIMEOUT:
      
// The process did not terminate within 5000 milliseconds.
      break;

   
case WAIT_FAILED:
      
// Bad call to function (invalid handle?)
      break;
}

3)简单说明WaitForMultipleObjects

DWORD WaitForMultipleObjects(DWORD dwCount,CONST HANDLE* phObjects,BOOL fWaitAll,DWORD dwMilliseconds);

参数dwCout:需要等待的内核对象的数量。
参数phObjects:需要等待的内核对象的是数组的指针。
参数fWaitAll:表示是否需要等待所有的内核对象。
参数dwMilliseconds:设置等待超时的时间。(同上函数)

返回值:WAIT_FAILED和WAIT_TIMEOUT同上函数。
 如果为fWaitAll参数传递TRUE,同时所有对象均变为已通知状态,那么返回值是WAIT_OBJECT_0。如果为fWaitAll传递FALSE       ,那么一旦任何一个对象变为已通知状态,该函数便返回。在这种情况下,你可能想要知道哪个对象变为已通知状态。返回值是WAIT_OBJECT_0与(WAIT_OBJECT_0+dwCount- 1)之间的一个值。

HANDLE h[3];
h[
0= hProcess1;
h[
1= hProcess2;
h[
2= hProcess3;
DWORD dw 
= WaitForMultipleObjects(3, h, FALSE, 5000);
switch(dw) 
{
   
case WAIT_FAILED:
      
// Bad call to function (invalid handle?)
      break;

   
case WAIT_TIMEOUT:
      
// None of the objects became signaled within 5000 milliseconds.
      break;

   
case WAIT_OBJECT_0 + 0:
      
// The process identified by h[0] (hProcess1) terminated.
      break;

   
case WAIT_OBJECT_0 + 1:
      
// The process identified by h[1] (hProcess2) terminated.
      break;

   
case WAIT_OBJECT_0 + 2:
      
// The process identified by h[2] (hProcess3) terminated.
      break;
}


二 参考msdn和windows核心编程。

posted on 2007-07-28 10:36 梦在天涯 阅读(6260) 评论(1)  编辑 收藏 引用 所属分类: CPlusPlus

评论

# re: C++多线程(五) 2009-06-10 11:16 aniki

关注中  回复  更多评论   


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理


公告

EMail:itech001#126.com

导航

统计

  • 随笔 - 461
  • 文章 - 4
  • 评论 - 746
  • 引用 - 0

常用链接

随笔分类

随笔档案

收藏夹

Blogs

c#(csharp)

C++(cpp)

Enlish

Forums(bbs)

My self

Often go

Useful Webs

Xml/Uml/html

搜索

  •  

积分与排名

  • 积分 - 1785089
  • 排名 - 5

最新评论

阅读排行榜