简体   繁体   English

WaitForSingleObject死锁

[英]WaitForSingleObject Deadlock

Some background if you're interested, otherwise you can go right to the question at the bottom: 如果您有兴趣,请提供一些背景知识,否则可以直接转到底部的问题:

I am running into an issue where i have an infinite while loop with two if conditions checking for two event object states to become signaled. 我遇到一个问题,我有一个无限的while循环,其中有两个if条件检查两个事件对象的状态是否发出信号。

while(1)
{
    if(DAQ_Comm_Server::usb_detect_flag == false)
    {
        if(WaitForSingleObject(USB_PHY_CONN,INFINITE) == WAIT_OBJECT_0)
        {
            DAQ_Comm_Server::usb_detect_flag = true;
        }
    }

    if(DAQ_Comm_Server::usb_detect_flag == true)
    {
        if(WaitForSingleObject(USB_PHY_DISCONN, INFINITE) == NULL)
        {
            DAQ_Comm_Server::usb_detect_flag = false;
        }   
    }
}

The event objects USB_PHY_CONN and USB_PHY_DISCONN are being set in my OS/BSP USB driver code where it will detect the hardware USB connection and proceed to use SetEvent() to set the corresponding event object. 在我的OS / BSP USB驱动程序代码中设置了事件对象USB_PHY_CONNUSB_PHY_DISCONN ,它将在其中检测硬件USB连接并继续使用SetEvent()设置相应的事件对象。

Reading the documentation for WaitForSingleObject() , it does not explicitly state that it must be used inside a thread, although after reading it multiple times I feel like it is implied, but im not 100% sure. 阅读WaitForSingleObject()的文档,它没有明确指出必须在线程内使用,尽管多次阅读后,我觉得它是隐含的,但不是100%肯定。

The issue I am having is the first time the code runs through the while loop (ie, usb is initially disconnected, then connected, and disconnected), my system runs fine and does not hang. 我遇到的问题是代码第一次通过while循环运行(即,usb最初已断开连接,然后再连接并断开连接),我的系统运行正常并且没有挂起。 However, Upon reconnecting the USB, my system freezes. 但是,重新连接USB后,我的系统死机了。 My device becomes unresponsive/UI freezes and the code is lost. 我的设备无响应/ UI冻结,并且代码丢失。

Now, the moment i kill the process where I have the above code in, everything starts back up and continues to run fine. 现在,当我杀死上面代码所在的进程时,一切开始备份并继续正常运行。 I did some reading and it seems that WaitForSingleObject() runs the risk of a possible deadlock, but I also noticed that it was always in terms of threads. 我做了一些阅读,似乎WaitForSingleObject()冒着可能出现死锁的风险,但是我也注意到,它总是在线程方面。

My question is , does WaitForSingleObject() HAVE to be used in a thread? 我的问题是 ,是否在线程中使用WaitForSingleObject() If i use it in an infinite loop in a main, does this run a high risk of a deadlock/system freezing? 如果我在主机的无限循环中使用它,是否会带来死锁/系统冻结的高风险?

Note: This is a platform builder windows embedded CE 7 project with VS2008. 注意:这是一个带有VS2008的Windows Builder CE 7项目的平台构建器。

You can't execute code outside a thread! 您不能线程执行代码! There's a "main" thread which starts at main but that too is a thread like any other. 有一个“主”线程从main开始,但是也像其他线程一样。

That said, a deadlock requires 2 threads and 2 synchronization points. 也就是说,死锁需要2个线程和2个同步点。 One thread locks A, another thread locks B, and then both threads block as they try to acquire the other lock. 一个线程锁定A,另一个线程锁定B,然后两个线程在尝试获取另一个锁定时都阻塞。

This can be solved absolutely by having a lock order. 这完全可以通过锁定命令来解决。 If lock A is always locked before B, then no deadlock can occur between threads that have A and other threads that have lock B. 如果锁A 始终在B之前锁定,则在具有A的线程与具有B的其他线程之间不会发生死锁。

A more theoretical approach proves that the problem is a cycle in the lock graph. 一种更理论的方法证明了问题是锁定图中的一个循环。 The cycle A<=>B is the simplest cycle of length 2. A->B->C->A can deadlock as well. 周期A <=> B是长度2的最简单周期。A-> B-> C-> A也可能死锁。 A Directed Acyclic Graph of lock orders corresponds to a deadlock-free program. 锁定顺序的有向无环图对应于无死锁程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM