?
Solved

simple multithreading question

Posted on 2002-06-27
19
Medium Priority
?
211 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 10
  • 4
  • 4
  • +1
19 Comments
 
LVL 86

Expert Comment

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

Expert Comment

by:jhance
ID: 7114890
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
ID: 7114918
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
Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

 

Author Comment

by:rshriner
ID: 7114927
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
ID: 7114928
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
ID: 7114947
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
ID: 7114953
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
ID: 7114963
OK, so the next obvious question - are you sure *all* of the handles are valid?
0
 

Author Comment

by:rshriner
ID: 7114971
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
 
LVL 86

Accepted Solution

by:
jkr earned 200 total points
ID: 7114976
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
ID: 7114981
Tried that, too. What an odd problem...
0
 
LVL 32

Expert Comment

by:jhance
ID: 7114987
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
ID: 7114995
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
ID: 7115004
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
ID: 7115088
#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
ID: 7115120
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
ID: 7115160
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
ID: 7118108
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
ID: 7121682
Thanks for your help!
0

Featured Post

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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.
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

719 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