Solved

simple multithreading question

Posted on 2002-06-27
19
205 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
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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Suggested Solutions

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

810 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