Is it safe to signal and immediately close a ManualResetEvent?

前端 未结 4 1334
借酒劲吻你
借酒劲吻你 2021-01-17 13:34

I feel like I should know the answer to this, but I\'m going to ask anyway just in case I\'m making a potentially catastrophic mistake.

The following code executes a

4条回答
  •  半阙折子戏
    2021-01-17 14:29

    My take is that there is a race condition. Having written event objects based on condition variables, you get code like this:

    mutex.lock();
    while (!signalled)
        condition_variable.wait(mutex);
    mutex.unlock();
    

    So while the event may be signalled, the code waiting on the event may still need access to parts of the event.

    According to the documentation on Close, this only releases unmanaged resources. So if the event only uses managed resources, you may get lucky. But that could change in the future so I would err on the side of precaution and not close the event until you know it is no longer being used.

提交回复
热议问题