🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: borland.public.cppbuilder.nativeapi
10 messages
10 total messages Started by mauro russo Sun, 06 Aug 2006 17:43
WaitForMulipleObjects function
#3233
Author: mauro russo
Date: Sun, 06 Aug 2006 17:43
23 lines
1807 bytes
Dear people,

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?

From the description in the Help (i opened it from the BCB 6 Update 4 IDE),
nowhere is clearry written this feature.
Really the sentence
<If bWaitAll is FALSE, the return value minus WAIT_OBJECT_0 indicates the
lpHandles array index of the object that satisfied the wait. If more than
one object became signalled during the call, this is the array index of the
signalled object with the smallest index value of all the signalled
objects.>
doesn't make me sure, when says "...of all the signalled objects".
Of course it says only 'signalled' and not 'acquired', so can't be wrong.

But i hope some of you can make me sure about my idea.

Best Regards,
Mauro.
Re: WaitForMulipleObjects function
#3234
Author: Bob Gonder
Date: Sun, 06 Aug 2006 22:17
29 lines
1693 bytes
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;
}
Re: WaitForMulipleObjects function
#3237
Author: 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;
> }
>
>
Re: WaitForMulipleObjects function
#3240
Author: mauro russo
Date: Mon, 07 Aug 2006 16:04
164 lines
5633 bytes
In the description of WaitforMultipleObjects
ther'is written nothing about Code Errors when fails.

Also the description of GetLastError reads
"For a complete list of error codes, see the WINNT.H header file in the
Win32 SDK."

I opened it but i didn't find anything listed as "code errors"
(i opened at the path C:\Programs\Borland\CBuilder6\Include\, that's
($BCB6)\Include )

I just found the constant SECURITY_SERVICE_RID with value 06L (i remember
you that 6 is
the value that GetLastError returns me).

Again, if i try to launch the same code (i posted in the precedent message
news:44d707a0@newsgroups.borland.com)
disablind "th1" (and disabling the waiting of the event by "th2"), the
thread "th2" can normally
acquires the first mutex whitout problems.

Some of you have same idea about the cause of the error descripted in the
message posted news:44d707a0@newsgroups.borland.com ?

Thank you,
Mauro.

"mauro russo" <m.russo@uniplan.it> ha scritto nel messaggio
news:44d707a0@newsgroups.borland.com...
> 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;
> > }
> >
> >
>
>
Re: WaitForMulipleObjects function
#3242
Author: Dennis Jones
Date: Mon, 07 Aug 2006 16:17
21 lines
1720 bytes
"mauro russo" <m.russo@uniplan.it> wrote in message 
news:44d764ad$1@newsgroups.borland.com...
> In the description of WaitforMultipleObjects
> ther'is written nothing about Code Errors when fails.
>
> Also the description of GetLastError reads
> "For a complete list of error codes, see the WINNT.H header file in the
> Win32 SDK."
>
> I opened it but i didn't find anything listed as "code errors"
> (i opened at the path C:\Programs\Borland\CBuilder6\Include\, that's
> ($BCB6)\Include )
>
> I just found the constant SECURITY_SERVICE_RID with value 06L (i remember
> you that 6 is
> the value that GetLastError returns me).

For error codes, see winerror.h

- Dennis 
Re: WaitForMulipleObjects function
#3241
Author: mauro russo
Date: Mon, 07 Aug 2006 16:34
150 lines
5124 bytes
Hi again!

I found that commenting the method Synchronize all worked correctly.
That call corrupted the stack and generated two not valid handles, so
the call to WaitForMultipleObjects of th2 generated error.

Well, ... removing the Synchronize call, i could see that
WaitForMultipleObjects,
with bWaitForAll = false, acquires ONLY ONE handle.

.... Thanks.


"mauro russo" <m.russo@uniplan.it> ha scritto nel messaggio
news:44d707a0@newsgroups.borland.com...
> 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;
> > }
> >
> >
>
>
Re: WaitForMulipleObjects function
#3243
Author: Bob Gonder
Date: Mon, 07 Aug 2006 16:51
20 lines
1593 bytes
mauro russo wrote:

>Really i hoped you know already.

One never really "knows" until they do it themselves.

>      if ( (w >= WAIT_OBJECT_0) & (w < WAIT_OBJECT_0 + NMsToWait) )

	Wrong "AND", use &&
>        ...
>For the thread "th2", the API fails [w = WAIT_FAILED] and GetLastError()
>gets the value six (6).

And....what does 6 mean?   (See FormatMessage() )
Thread 1 exits after accepting mutex1, which closes that handle.
Thread2 then attempts to use the closed handle.

>some help for me?

Run my code.
Re: WaitForMulipleObjects function
#3245
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Aug 2006 17:58
10 lines
1216 bytes
"Bob Gonder" <notbg@notmindspring.invalid> wrote in message
news:nvped25gk8hs0bdhpkb8a4mmnlau7akc8c@4ax.com...

> And....what does 6 mean?   (See FormatMessage() )

6 = ERROR_INVALID_HANDLE


Gambit
Re: WaitForMulipleObjects function
#3244
Author: Remy Lebeau (TeamB)
Date: Mon, 07 Aug 2006 18:00
22 lines
1702 bytes
"mauro russo" <m.russo@uniplan.it> wrote in message
news:44d62a4a$1@newsgroups.borland.com...

> 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?

Yes, that is exactly what it does.  You would have to call a WaitFor...()
function again to acquire the other objects separately.

> doesn't make me sure, when says "...of all the signalled objects".
> Of course it says only 'signalled' and not 'acquired'

It is all the same thing as far as the waiting functions are concerned.
Some objects produce signals internally, and some acquire locks internally.
Don't worry about it too much.


Gambit
Re: WaitForMulipleObjects function
#3249
Author: mauro russo
Date: Mon, 07 Aug 2006 18:47
22 lines
1706 bytes
"Remy Lebeau (TeamB)" <no.spam@no.spam.com> ha scritto nel messaggio
news:44d7801a$1@newsgroups.borland.com...
>
> "Bob Gonder" <notbg@notmindspring.invalid> wrote in message
> news:nvped25gk8hs0bdhpkb8a4mmnlau7akc8c@4ax.com...
>
> > And....what does 6 mean?   (See FormatMessage() )
>
> 6 = ERROR_INVALID_HANDLE

    In fact. By more deep debugging i could realize it.
    The cause was (i think) in the calling of Synchronize method to call
ShowMessage,
    that corrupt the stack where is located the HANDLE array.
    After i removed it, i could see all working.

>
> Gambit


Thanks.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads