Solved

simple multithreading question

Posted on 2002-06-27
19
202 Views
Last Modified: 2013-11-20
I am attempting to write a simple dialog-based application that communicates with other console-based applications over Named Pipes IPC. In my MFC app, I make a call to WaitForMultipleObjects(). Unfortunately, thr call never blocks/waits like it should! I never get the correct return value. In fact, I tested the return value and it is always 'WAIT_FAILED'. Also, the error message associated with GetLastError() is 'The handle is invalid'. Can anyone help? This is very frustrating. Especially when I know that similar code works in several of my console-based apps. As a side note, I tried to get my app to work by waiting on a single object with WaitForSingleObject(). This seemed to work just fine. So, why won't a call to WaitForMultipleObjects() work? Please help!
0
Comment
Question by:rshriner
  • 10
  • 4
  • 4
  • +1
19 Comments
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Could you post some code? Sorry for the inconvenience, but my crystal ball is on repair...
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
What that message is saying is that the HANDLE you are passing to WaitForSingleObject() is not a valid handle.  

How are you getting this handle?

WaitForSingleObject() can't wait on something that does not exist.
0
 

Author Comment

by:rshriner
Comment Utility
Sure. Here's the thread function that is launched from my Dialog's OnInitDialog() function:

void CNptestDlg::npRun()
{
     // DO SOMETHING (this is the MEMBER thread function)

     CString temp;
     DWORD     result;

     HANDLE msgSignal[] = {
          msg[SUBSYSTEM_STATUS].signalingEvent,
          msg[VEHICLE_INFO].signalingEvent,
          msg[WARNING].signalingEvent,
          msg[VIDEO_REQUEST].signalingEvent
     };

    if( ! startMsgThread() )
    {
          return;// -1;
     }

     Sleep(2000);

     while( TRUE )
     {
          result = WaitForMultipleObjects( (sizeof(msgSignal)/sizeof(HANDLE)), msgSignal, FALSE, INFINITE) - WAIT_OBJECT_0;

          if (result == WAIT_FAILED)
               TRACE("Result: (%d) WAIT_FAILED\n", result);

          FormatErrorMsg();

          // Get current time
          //CurrentTime = GetTickCount();

          switch( result )
          {
               case 0:     // SUBSYSTEM_STATUS
                    WaitForSingleObject(msg[SUBSYSTEM_STATUS].lockingMutex, INFINITE);
                    temp.Format("0x92: %X", gv92_Subsystem());
                    ReleaseMutex(msg[SUBSYSTEM_STATUS].lockingMutex);
                    UpdateControl(temp);
                    break;

               case 1:     // VEHICLE_INFO
                    WaitForSingleObject(msg[VEHICLE_INFO].lockingMutex, INFINITE);
                    temp.Format("0x50: %X (speed)", gv50_Speed());
                    ReleaseMutex(msg[VEHICLE_INFO].lockingMutex);
                    UpdateControl(temp);
                    break;

               case 2:     // WARNING
                    WaitForSingleObject(msg[WARNING].lockingMutex, INFINITE);
                    temp.Format("0x51: %X", gv51_Warning());
                    ReleaseMutex(msg[WARNING].lockingMutex);
                    UpdateControl(temp);
                    break;

               case 3:     // VIDEO_REQUEST
                    WaitForSingleObject(msg[VIDEO_REQUEST].lockingMutex, INFINITE);
                    temp.Format("0x20: %X", gv20_VideoRequest());
                    ReleaseMutex(msg[VIDEO_REQUEST].lockingMutex);
                    UpdateControl(temp);
                    break;

               default: // return value is invalid
//                    printf("Wait error: %d\n", GetLastError());
//                    ExitProcess(0);
                    break;
          }
     }

     running = FALSE;
}
0
 

Author Comment

by:rshriner
Comment Utility
If I wait on any single one of the signalingEvents listed using WaitForSingleObject(), it will work. Does this mean I'll have to create multiple threads that call WaitForSingleObject() for EVERY event I need to wait for?
0
 

Author Comment

by:rshriner
Comment Utility
If I wait on any single one of the signalingEvents listed using WaitForSingleObject(), it will work. Does this mean I'll have to create multiple threads that call WaitForSingleObject() for EVERY event I need to wait for?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
There we have the problem:

         result = WaitForMultipleObjects( (sizeof(msgSignal)/sizeof(HANDLE)), msgSignal, FALSE, INFINITE) - WAIT_OBJECT_0;

Avoid to substract WAIT_OBJECT_0 from the return value, make it

         result = WaitForMultipleObjects( (sizeof(msgSignal)/sizeof(HANDLE)), msgSignal, FALSE, INFINITE);

        if (result == WAIT_FAILED)
              TRACE("Result: (%d) WAIT_FAILED\n", result);
        else result -= WAIT_OBJECT_0;

BTW, WAIT_FAILED is defined as 0xffffffff...

0
 

Author Comment

by:rshriner
Comment Utility
Nope. Doesn't appear to work. I wish it did, though. BTW, WAIT_OBJECT_0 is defined as 0.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
OK, so the next obvious question - are you sure *all* of the handles are valid?
0
 

Author Comment

by:rshriner
Comment Utility
Yes. I tried them individually with WaitForSingleObject(). I'm fairly baffled by this problem... like I said before, I use similar code in several console-based apps and they work just fine...
0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
Comment Utility
An 'esoteric' shot in the dark - try to hard-code the number of handles as '4'. Maybe there is some alignment side-effect...
0
 

Author Comment

by:rshriner
Comment Utility
Tried that, too. What an odd problem...
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Two things from the SDK docs:

1) It may not contain the multiple copies of the same handle.

2) If one of these handles is closed while the wait is still pending, the function's behavior is undefined.

What about #2?
0
 

Author Comment

by:rshriner
Comment Utility
jhance, I'm a bit confused... what do mean by #2? These handles remain open for the duration of the threads execution...
0
 

Author Comment

by:rshriner
Comment Utility
jhance, I'm a bit confused... what do mean by #2? These handles remain open for the duration of the threads execution...
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
#2 - If one of these handles is closed while the wait is still pending, the function's behavior is undefined.

>>>These handles remain open for the duration of the threads execution

Are you SURE?  How do you know?  It seems to me that for someone who doesn't know where the problem is you sure seem confident about where it ISN'T!!
0
 

Author Comment

by:rshriner
Comment Utility
OOPS! I just figured it out! I made a call to a startMsgThread() that allocates and initializes the signalingEvents AFTER the populated the msgEvent array. Thus, the msgEvent array elements were all NULL and WaitForMultipleObjects() had nothing to wait on! Boy, I sure wasted some time on this one! Sorry, guys! Hey, is there anyway I can split the points? Give you guys each a few for helping me out? I've only used EE a couple of times and I can't remember how to do something like that (if you even can).
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Glad you found the problem.  Don't you just hate it when the GetLastError() tells you just exactly what the problem is?

POst a 0 pt Question in the Community Support topic area and ask a moderator to slpit things up however you wish.
0
 
LVL 5

Expert Comment

by:Netminder
Comment Utility
I have reduced the points in the question to 50, so you can now finish the split. You should now take the following steps.

1. Post a question in this topic area for either jhance or jkr. Refer to this question in the body of the question, and set the point value at 50.

2. Select the response of the other Expert as the Answer for this question.

3. Post the URL of the new "points for" question in this question.

Netminder
CS Moderator
0
 

Author Comment

by:rshriner
Comment Utility
Thanks for your help!
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
notReplace  challenge 53 102
either24  challenge 19 84
matchUp  challenge 6 48
How to convert MFC::CString to UTF8 wchar_t* 10 92
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now