🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #3237

Re: WaitForMulipleObjects function

#3237
From: mauro russo
Date: Mon, 07 Aug 2006 09:27
131 lines
4347 bytes
Really i hoped you know already.
However i build an easy test, but i'm having a weird (for me) result:

That's the code:


    HANDLE Handle_M1 = CreateMutex(NULL,false,"M1");
    HANDLE Handle_M2 = CreateMutex(NULL,false,"M2");

   HANDLE Handle_ev2 = CreateEvent(NULL,false,false,"ev2");

   HANDLE Ms[2] = {Handle_M1,Handle_M2};

   TThreadWMO *th1 = new TThreadWMO(true);
   th1->EventToWaitAtBegin = NULL;
   th1->EventToSignalAtEnd = Handle_ev2;
   th1->MsToWait = Ms;
   th1->NMsToWait = 2;
   th1->Name = "th1";

   TThreadWMO *th2 = new TThreadWMO(true);
   th2->EventToWaitAtBegin = Handle_ev2;
   th2->EventToSignalAtEnd = NULL;
   th2->MsToWait = Ms;
   th2->NMsToWait = 2;
   th2->Name = "th2";

   th1->Resume();
   th2->Resume();
}

where:

class TThreadWMO : public TThread
{
private:
protected:
   void __fastcall Execute();
   String MsgToshow;
   void __fastcall ShowMsg(void);
public:
   String Name;
   HANDLE EventToWaitAtBegin,EventToSignalAtEnd;
   CONST HANDLE *MsToWait;
   unsigned int NMsToWait;
   __fastcall TThreadWMO(bool CreateSuspended);
};

__fastcall TThreadWMO::TThreadWMO(bool CreateSuspended)
   : TThread(CreateSuspended)
{
   EventToWaitAtBegin = EventToSignalAtEnd = NULL;
   MsToWait = NULL;
   NMsToWait = 0;
}

void __fastcall TThreadWMO::Execute()
{
   //---- Place thread code here ----
   if (EventToWaitAtBegin != NULL)
      WaitForSingleObject(EventToWaitAtBegin,INFINITE);

   if (NMsToWait > 0){
      DWORD w = WaitForMultipleObjects(NMsToWait,MsToWait,false,INFINITE);
//waits at least one.
      if (w == WAIT_TIMEOUT)
         ...
     else
      if ( (w >= WAIT_OBJECT_0) & (w < WAIT_OBJECT_0 + NMsToWait) )
        ...
    else
      if ( (w >= WAIT_ABANDONED_0) & (w < WAIT_ABANDONED_0 + NMsToWait) )
         ...
      else
      if (w == WAIT_FAILED)
         ...
      else
         ...
   }

   if (EventToSignalAtEnd != NULL)
      SetEvent(EventToSignalAtEnd);
}

Well. For the thread "th1" the API WaitForMultipleObjects successes with w =
WAIT_OBJECT_0 (until here it's what we except and doesn't add anything to
answer to my question).
For the thread "th2", the API fails [w = WAIT_FAILED] and GetLastError()
gets the value six (6).

1) What can make it failing??????
2) i couldn't answer to my initial question, because the API is for "th2" is
failing. I hoped to see w = WAIT_OBJECT_0+1.

some help for me?

Thank you.

"Bob Gonder" <notbg@notmindspring.invalid> ha scritto nel messaggio
news:prpcd2hj1di4dpm4uqun57ncaj7g69fkl9@4ax.com...
> mauro russo wrote:
>
> >if i use the value FALSE  for  the bWaitAll parameter
> >and more objects are signalled, i expect that the thread that
> >called WaitForMultipleObjects acquires only one [that with the
> >minimum index] of those objects.
> >Is it like that?
> >
> >But i hope some of you can make me sure about my idea.
>
> Test it.
>
> #include <stdio.h>
> #include <conio.h>
> #include <windows.h>
> int main(void)
> { int i;
> DWORD w;
> HANDLE  Events[5];
> for( i=0; i<5; ++i )
> Events[i]=CreateEvent(NULL,FALSE,TRUE,NULL);
> do{
> w = WaitForMultipleObjects( 5, Events, FALSE, 1000 );
> if( w >= WAIT_OBJECT_0 )
> printf( "Event %i Reported\r\n", w-WAIT_OBJECT_0 );
> }while( w !=WAIT_TIMEOUT );
> getch();
> return 0;
> }
>
>
Technical Details
Message-ID: <44d707a0@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <44d62a4a$1@newsgroups.borland.com> <prpcd2hj1di4dpm4uqun57ncaj7g69fkl9@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,8747f5d210339525
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
Reply-To: "mauro russo" <m.russo@uniplan.it>
From: "mauro russo" <m.russo@uniplan.it>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <44d62a4a$1@newsgroups.borland.com> <prpcd2hj1di4dpm4uqun57ncaj7g69fkl9@4ax.com>
Subject: Re: WaitForMulipleObjects function
Date: Mon, 7 Aug 2006 11:27:29 +0200
Lines: 133
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1106
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1106
NNTP-Posting-Host: 217.20.0.176
Message-ID: <44d707a0@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1154942880 217.20.0.176 (7 Aug 2006 02:28:00 -0700)
Path: arc1-ca.pugleaf.net!g2news2.google.com!news3.google.com!border1.nntp.dca.giganews.com!nntp.giganews.com!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail