Article View: borland.public.cppbuilder.nativeapi
Article #14224Re: SetEvent
From: Ralph Kazemier
Date: Thu, 21 Aug 2003 14:50
Date: Thu, 21 Aug 2003 14:50
115 lines
3743 bytes
3743 bytes
"Eyyub Volkan ๏ฟฝektimur" <evcektimur@prokon.com.tr> wrote in message
news:3f44c115@newsgroups.borland.com...
> I'm using windows SetEvent method , i need to wait triggered event
> finishs the operation. but SetEvent do not wait triggered event.
SetEvent() triggers the event where other threads are waiting for using
WaitForSingleObject().
> Is there any way to do this ?
> SetEvent(hEvent);
> Wait for finish.... ???
If you want one thread to signal an event and then wait until another thread
has responded to that event, you'll need a second event the calling thread
can wait for and the waiting thread can signal. E.g.;
thread A:
---------
SetEvent(hEvent);
WaitForSingleObject(hEventHandled);
thread B:
---------
WaitForSingleObject(hEvent);
// do stuff
SetEvent(hEventHandled);
This only works with two threads. When you want more threads to wait for
the event, you'll need some sort of registration mechanism. This mechanism
will allow multiple threads to register the same event and one particular
thread (the owner) to signal it. When the owner signals the event, it then
waits until all registered threads notify that they have handled the event.
Thread A creates the event. Thread B, C and D register the event and wait
for it. At some point thread A signals the event. Thread B, C and D
respond to that and when they are done they unregister the event. When all
threads have unregistered the event, 'thread A is signalled'.
This is just a draft, the registration mechanism is very poor and not
foolproof, but it might give you an idea on how to proceed;
class MultiEvent {
private:
HEVENT m_hEvent;
HEVENT m_hEventHandled;
ULONG m_ulRefCount;
public:
MultiEvent(): m_ulRefCount(0) {
m_hEvent = CreateEvent(
NULL,
TRUE, /* manual reset */
FALSE,
NULL
);
m_hEventHandled = CreateEvent(
NULL,
FALSE,
FALSE,
NULL
);
}
~MultiEvent() {
CloseHandle(m_hEvent);
CloseHandle(m_hEventHandled);
}
DWORD Signal() {
SetEvent(m_hEvent);
WaitForSingleObject(m_hEventHandled);
}
DWORD Register() {
InterlockedIncrement(&m_ulRefCount);
return WaitForSingleObject(m_hEvent, INFINITE);
}
void Unregister() {
if(!InterlockedDecrement(&m_ulRefCount)) {
ResetEvent(m_hEvent);
SetEvent(m_hEventHandled);
}
}
};
thread A:
MultiEvent* e = new MultiEvent();
// do stuff
e->Signal();
thread B:
e->Register();
// handle event
e->Unregister();
thread C:
e->Register();
// handle event
e->Unregister();
thread D:
e->Register();
// handle event
e->Unregister();
Ralph
Technical Details
Message-ID:
<3f44db03@newsgroups.borland.com>Path:
arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!newsfeed.news2me.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mailReferences:
<3f44c115@newsgroups.borland.com>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII X-Google-Thread: 10cc67,2cb89a70dc179873 X-Google-Attributes: gid10cc67,public X-Google-ArrivalTime: 2003-08-21 07:53:43 PST From: "Ralph Kazemier" <ralph.kazemier@jess.nl> Newsgroups: borland.public.cppbuilder.nativeapi References: <3f44c115@newsgroups.borland.com> Subject: Re: SetEvent Date: Thu, 21 Aug 2003 16:50:13 +0200 Lines: 117 X-Priority: 3 X-MSMail-Priority: Normal X-Newsreader: Microsoft Outlook Express 6.00.2600.0000 X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2600.0000 NNTP-Posting-Host: 193.172.20.64 Message-ID: <3f44db03@newsgroups.borland.com> X-Trace: newsgroups.borland.com 1061477123 193.172.20.64 (21 Aug 2003 07:45:23 -0700) Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-06!sn-xit-09!supernews.com!newsfeed.news2me.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail