Windows下 线程等待事件句柄和主线程等待子线程句柄的区别?

sharpff 2008-10-03 11:28:55
等待函数WaitForSingleObject(hEvent/hThread, INFINITE);
为什么主线程等待hThread就会阻塞式的等到子线程执行完毕?
然而,等待事件句柄hEvent就需要放在while(1)里面反复的等?
ps:WaitForSingleObject还会等待mutex, 还有什么?
...全文
110 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
sharpff 2008-10-04
  • 打赏
  • 举报
回复
感谢weiyijiji的回答,很到位。
chlaws 2008-10-03
  • 打赏
  • 举报
回复
The WaitForSingleObject function can wait for the following objects:

Change notification
Console input
Event
Job
Mutex
Process
Semaphore
Thread
Waitable timer
hopeclass_shych 2008-10-03
  • 打赏
  • 举报
回复
学习。好好看看MSDN就有答案了
baihacker 2008-10-03
  • 打赏
  • 举报
回复
Waits until the specified object is in the signaled state or the time-out interval elapses.

To enter an alertable wait state, use the WaitForSingleObjectEx function. To wait for multiple objects, use the WaitForMultipleObjects.


DWORD WINAPI WaitForSingleObject(
__in HANDLE hHandle,
__in DWORD dwMilliseconds
);

Parameters
hHandle
A handle to the object. For a list of the object types whose handles can be specified, see the following Remarks section.

If this handle is closed while the wait is still pending, the function's behavior is undefined.

The handle must have the SYNCHRONIZE access right. For more information, see Standard Access Rights.

dwMilliseconds
The time-out interval, in milliseconds. The function returns if the interval elapses, even if the object's state is nonsignaled. If dwMilliseconds is zero, the function tests the object's state and returns immediately. If dwMilliseconds is INFINITE, the function's time-out interval never elapses.

Return Value
If the function succeeds, the return value indicates the event that caused the function to return. It can be one of the following values.

Return code/value Description
WAIT_ABANDONED
0x00000080L
The specified object is a mutex object that was not released by the thread that owned the mutex object before the owning thread terminated. Ownership of the mutex object is granted to the calling thread, and the mutex is set to nonsignaled.

If the mutex was protecting persistent state information, you should check it for consistency.

WAIT_OBJECT_0
0x00000000L
The state of the specified object is signaled.

WAIT_TIMEOUT
0x00000102L
The time-out interval elapsed, and the object's state is nonsignaled.


If the function fails, the return value is WAIT_FAILED ((DWORD)0xFFFFFFFF). To get extended error information, call GetLastError.

Remarks
The WaitForSingleObject function checks the current state of the specified object. If the object's state is nonsignaled, the calling thread enters the wait state until the object is signaled or the time-out interval elapses.

The function modifies the state of some types of synchronization objects. Modification occurs only for the object whose signaled state caused the function to return. For example, the count of a semaphore object is decreased by one.

The WaitForSingleObject function can wait for the following objects:


Change notification
Console input
Event
Memory resource notification
Mutex
Process
Semaphore
Thread
Waitable timer
baihacker 2008-10-03
  • 打赏
  • 举报
回复
因为你都说了,要无限地等下去:INFINITE

这些都取决于你的应用.
csucdl 2008-10-03
  • 打赏
  • 举报
回复
这个没什么神奇的,需要知道对各个同步对象signaled, unsigaled状态的定义。
weiyijiji 2008-10-03
  • 打赏
  • 举报
回复
这不是hThread和hEvent的问题.当你设置INFINATE时,只要内核对象还没有处于受信状态,就会一直等待下去.while(1)可能因为等到hEvent还要做某些操作.
但是hThread是不能这么等的,因为第1次等到后,那个线程就已经退出了,没有再等的必要.
可以等待很多内核对象.

64,637

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧