English 中文(简体)
在叫ResetEvent(ResetEvent)时,我是否需要变换或锁定?
原标题:Do I need to mutex or lock when calling ResetEvent()?

我继承了这样的一些法典:

m_mutex.Lock();
ResetEvent( m_hSyncObject );
m_mutex.Unlock();

缩略语

Are those Mutexes necessary in this case - do those calls behave themselves or can I get away with removing the locks? This function has already had some inc/decs of values that I made atomic earlier and now just these events are within the locks, so getting rid of them would be a big win if possible.

最佳回答

这一额外平衡几乎是不需要的。 <代码>ResetEvent和SetEvent功能本身可安全从多个深层接通

Given that this code does exists it seems highly likely that the developer who wrote that code didn t understand the threading semantics they d created. I would treat any code depending on that logic as highly suspect. It d probably save you some time in the long run to go ahead and pre-audit that code for threading issues

问题回答

Cassat Programmer!

Manual-reset events are hard to use and may require you to hold a lock around setting and resetting the event (auto-reset events make it easier to avoid these issues).

Consider this code:

Worker() {
    WaitForSingleObject(hEvent);
    DoWork();
    ResetEvent(hEvent);
}

EventThread() {
    QueueWork();
    SetEvent(hEvent);
}

可能的话是,工人在活动<>之后重新开始活动。 在这种情况下,为了妥善利用人工查询活动,你需要24小时接手重新启动活动,并用重新确定活动的方式检查点的状况。

自动启动的活动使你能够原谅并重新启动避免这种种族的活动(如果你在工作时已经耗尽了一定时间,但你却不听从任何警示)。

Events are atomic, so there s no need to use a mutex around a SetEvent or ResetEvent, unless there s something else along with it and the two have to be done atomically (e.g., if you set one event and reset another).





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签