English 中文(简体)
放弃WaitForMultipleObjects
原标题:Waiting on WaitForMultipleObjects

I m试图为我的<代码>FileWatcher类别撰写一个单位测试。

源自校对班和使用<代码>。 WaitForMultipleObjects ,以待在其校读程序中处理两件:

  1. The handle returned from FindFirstChangeNotification
  2. A handle for an Event that lets me cancel the above wait.

因此,基本上,正在等待任何情况:档案变更或我告诉它停止观察。

现在,在试图写造法时,我需要等待它开始等待。

《白奴法典》:

FileWatcher.Wait(INFINITE)
ChangeFile()
// Verify that FileWatcher works (with some other event - unimportant...)

问题是,存在着种族条件。 首先,我需要确保FilWatcher开始等待(即现在其胎面在<代码>上被阻断)。 WaitForMultipleObjects之前,我可以启动第2行的档案改动。 我不想使用。 睡觉,因为睡觉似乎很 ha,在偷渡时,必然给我带来问题。

我很熟悉<代码>SignalObjectAndWait,但它确实没有解决我的问题,因为我需要“SignalObjectAndWaitOnMultipleObjects”。

任何想法?


<><>Edit>/strong>

为澄清一个范围,此处为<代码>的简化版本。 文件:

// Inherit from this class, override OnChange, and call Start() to turn on monitoring. 
class FileChangeWatcher : public Utils::Thread
{
public:
    // File must exist before constructing this instance
    FileChangeWatcher(const std::string& filename);
virtual int Run();
    virtual void OnChange() = 0;
};

它继承了<条码>Thread,并履行阅读功能,如此(非常简化):

_changeEvent = ::FindFirstChangeNotificationW(wfn.c_str(), FALSE, FILE_NOTIFY_CHANGE_LAST_WRITE);

HANDLE events[2] = { _changeEvent, m_hStopEvent };

DWORD hWaitDone = WAIT_OBJECT_0;
while (hWaitDone == WAIT_OBJECT_0)
{
    hWaitDone = ::WaitForMultipleObjects(2, events, FALSE, INFINITE);

    if (hWaitDone == WAIT_OBJECT_0)
        OnChange(); 
    else
        return Thread::THREAD_ABORTED;
} 
return THREAD_FINISHED;

通知说,校对功能等待两种操作,一种是变更通知,另一种是“站式”活动(从校对传)。

现在,测试这一类的法典就是这样:

class TestFileWatcher : public FileChangeWatcher
{
public:
    bool Changed;
    Event evtDone;
    TestFileWatcher(const std::string& fname) : FileChangeWatcher(fname) { Changed = false; }
    virtual void OnChange()
    {
        Changed = true;
        evtDone.Set();
    }
};

CNPUnit测试援引:

std::string tempFile = TempFilePath();
StringToFile("Hello, file", tempFile);
TestFileWatcher tfw(tempFile);
tfw.Start();
::Sleep(100); // Ugly, but we have to wait for monitor to kick in in worker thread
StringToFile("Modify me", tempFile);
tfw.evtDone.Wait(INFINITE);
CPPUNIT_ASSERT(tfw.Changed);

想法是在中间摆脱这种睡觉。

最佳回答

在没有种族的情况下,你不必等到进入。 如果你在要求履行这一职能之前作出这一改变,那将立即返回。

<><>Edit>: 现在我可以看到种族。 为什么不把你推向下行:

_changeEvent = ::FindFirstChangeNotificationW(/*...*/);

<代码>的校对功能 文件ChangeWatcher? 这样,你就可以确定,到“<编码> 规范ToFile功能”时,档案已经受到监视。

问题回答

您请在座标的构造中打上<条码>,并储存其交回的手工艺,供您使用。 这意味着,从施工开始,你将迎合变化。

一旦你们的read子开始,就只是要等待两个手。 如果在校正开始之前发生了变化,那么该校正就会处理<代码>。 退回的FindFirstChangeNoification()将发出信号,并将处理这一改动。 如果您愿意监测许多变化,则应在每份通知处理后,填写和打电话<条码>。

相反,你可以使用Mustex。 在能够利用它所希望的资源之前,它必须锁定Mutex,并锁定需要资源的其他线索。

呼吁创建企业,以创建一个没有签字的活动。 当观察者进入其主要通道时,SetaEvent(不管怎样)。 与此同时,在FilWatcher的首批WaitForSingleObject()中,一旦返回,世界自然及自然资源保护组织就象你以前所做的那样。





相关问题
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?

热门标签