Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 215
  • Last Modified:

simple multithreading question

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
rshriner
Asked:
rshriner
  • 10
  • 4
  • 4
  • +1
1 Solution
 
jkrCommented:
Could you post some code? Sorry for the inconvenience, but my crystal ball is on repair...
0
 
jhanceCommented:
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
 
rshrinerAuthor Commented:
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
rshrinerAuthor Commented:
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
 
rshrinerAuthor Commented:
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
 
jkrCommented:
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
 
rshrinerAuthor Commented:
Nope. Doesn't appear to work. I wish it did, though. BTW, WAIT_OBJECT_0 is defined as 0.
0
 
jkrCommented:
OK, so the next obvious question - are you sure *all* of the handles are valid?
0
 
rshrinerAuthor Commented:
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
 
jkrCommented:
An 'esoteric' shot in the dark - try to hard-code the number of handles as '4'. Maybe there is some alignment side-effect...
0
 
rshrinerAuthor Commented:
Tried that, too. What an odd problem...
0
 
jhanceCommented:
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
 
rshrinerAuthor Commented:
jhance, I'm a bit confused... what do mean by #2? These handles remain open for the duration of the threads execution...
0
 
rshrinerAuthor Commented:
jhance, I'm a bit confused... what do mean by #2? These handles remain open for the duration of the threads execution...
0
 
jhanceCommented:
#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
 
rshrinerAuthor Commented:
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
 
jhanceCommented:
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
 
NetminderCommented:
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
 
rshrinerAuthor Commented:
Thanks for your help!
0

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 10
  • 4
  • 4
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now