为什么将事件对象假定在线程函数中有效?

发布于 2025-01-26 21:34:04 字数 847 浏览 4 评论 0原文

Winapi中假定在线程函数中有效的Winapi中的句柄事件对象(由CreateEvent函数创建的同步对象(同步对象)?

来自多intionReading示例 to Microsoft docs代码示例,此事件对象传递给没有任何保护的waitforsingleobject函数。

我一直在做同样的事情。今天,我刚刚想到的是,从代码的角度来看,我该如何安全地处理这个“分支”安全。

从严格的意义上讲,此事件对象沿多个线程共享,至少在调用setEvent的线程中,并在调用waitforsingleobject的线程中共享。

因此,必须将其归类为一种共享资源。然后,所有共享资源都必须受“锁定”的保护,例如MUTEX或关键部分。

此外,在线程还活着时,可以在SetEvent之后故意调用CloseHandle,这将导致通过封闭的事件句柄在线程函数中waitforsingleobject。 (也许由于递延删除而不会删除事件对象)

在线程功能中获取锁定并调用waitforsingleobject,并尝试在其他线程中获取锁定以调用setEvent肯定会导致死锁。

[编辑]

也许我通过提及“假设”和特定的代码示例来误导了我的观点。我想知道如何进行线程安全有效性检查手柄事件对象,将手柄视为可变。

Why is HANDLE event object(synchronization object which is created by CreateEvent function) in winapi assumed to be valid in thread function?

From multithreading example to microsoft docs code examples, this event object is passed to WaitForSingleObject function without any protection.

I've been doing the same. And today, I just reached to the thought that how can I deal with this "branch" safe, in such a sense like branch coverage in code perspective.

In the strict sense, this event object is shared along multiple threads, at least in the thread which calls SetEvent and in the thread which Calls WaitForSingleObject.

Therefore, it has to be classified as a type of shared resource. Then, all shared resources must be protected by "lock", such as mutex or critical section.

Also, it is possible to deliberately call CloseHandle after SetEvent while thread is alive, which will lead to passing closed event handle to WaitForSingleObject in thread function. (maybe the event object won't be deleted due to deferred deletion)

Acquiring lock and calling WaitForSingleObject in thread function, and trying to acquire lock in other thread in order to call SetEvent would definitely lead to deadlock.

[EDIT]

Maybe I misled my point by mentioning "assumed" and particular code example. I wonder how to do thread safe validity check for HANDLE event object, treating HANDLE as variable.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

尛丟丟 2025-02-02 21:34:04

根据,有许多对象可以使用其处理来同步多个线程。这些对象包括:

  • 控制台输入缓冲区
  • 事件
  • 互斥轴
  • 的过程
  • 螺纹
  • 螺纹
  • 计时器

这些对象的状态均为信号或未发出信号

。 Learn..microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-waitforsingleobject'仍在待处理,该函数的行为是不确定的。
对于无效的手柄,在句柄变得无效的情况下进行故障排除是程序员的责任( bug )。

According to Synchronizing Execution of Multiple Threads, There are a number of objects whose handles can be used to synchronize multiple threads. These objects include:

  • Console input buffers
  • Events
  • Mutexes
  • Processes
  • Semaphores
  • Threads
  • Timers

The state of each of these objects is either signaled or not signaled.(atomic)

For handle concerned, WaitForSingleObject function say If this handle is closed while the wait is still pending, the function's behavior is undefined.
For an invalid handle, It's programmer's responsibility to troubleshoot where the handle becomes invalid(BUG).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文