Solved

Multithread problem (SetEvent), URGENT!!

Posted on 1998-11-01
3
646 Views
Last Modified: 2013-11-20
My project consists of 2 parts:
a/ the main processing part (MPP), which handles 2 kinds of
   eternal messages,
b/ the wait part (WP), that waits and get the 2 kinds of
   external messages.
   
The mechanism is like this:
1) in main(), in main.c, which is the (MPP), create 2 events in order to distinguish the 2 kinds of messages, onfirmation and indication:

main(){
// 1st kinda message: CONF, confirmation
hEventWaitConf = CreateEvent(NULL,      
                  FALSE,      
                  FALSE,      
                  "EventWaitConf");
// 2nd kinda message: IND, indication, inwhich, it contains
// several kinds of micro_message, that I have to choose.
hEventWaitInd = CreateEvent(NULL,  
                  FALSE,
                  FALSE,
                  "EventWaitInd");

2) begin a thread to do the wait job, which is called, (WP)
_beginthread( WaitLoop, 0, NULL );

// where the wait loop is also in main.c, while the EventLoop() is
// in another file called event.c
void WaitLoop(void *dummy )
{
    extern void    EventLoop(void);
      for(;;)
      {
            EventLoop();
            if ( glb_shutdown == 2 ) break;
      }            
      /* _endthread given to terminate */    
      _endthread();
}


======= in event.c

void EventLoop()
{      :
      :
      GET_MESSAGE(the_queue, msg);
      if ( the_que == QueueEmpty )
      {
            WAIT_FOR_SIGNAL ();
      }
      else
      {
            if ( msg == CONF )
            {                  
                  SetEvent(hEventWaitConf);
            }                        

            if ( msg == IND )
            {                  
                  // the ProcessEvent() is to analyse                         // the micro_msg to see
                  // if this is the one I wnat.
                  ProcessEvent(msg, micro_msg);
                  if (micro_msg == the_wanted_msg)
                  {
                        SetEvent(hEventWaitInd);
                  }
            } //if
      } // else
return;
}

3)and in my processing procedure, i have to wait for these IND or CONF arrives
  b4 I proceed, so:
      :
      :// these process may or may not in main.c
      processA;

      WaitForSingleObject(hEventWaitConf,INFINITE);
      
      WaitForSingleObject(hEventWaitInd,4000);
      
      processB;
      :
      :

/**************************************
My problem is:
the SetEvent(hEventWaitInd) don't always work!
soemtimes the WaitforSingleObject(hEventInd) cannot be set in the functions,
and sometimes, it was 'mysteriously' set <-- this may due to the fact
that, it was set the 1st time, and did not reset itself, so for the 2nd time
i wait for it, it was already set. (dunno why coz I when i create this event,
it set it as FAULSE)

Will it be the SetEvent() is placed in different functions in different files?

1 more thing, one I was in the main(), waiting the single object to be set,
the wait thread (WP) was also halted.

Very frustrated, pls help!!!!

Thanks in advance.

0
Comment
Question by:p1
  • 2
3 Comments
 
LVL 1

Expert Comment

by:gvg
ID: 1324107
This is not very understandable but I am trying to understand this.  First where does the WP get its messages from.  If it is the MPP then you are in danger of Deadlock in case of both threads waiting.

I have one idea though about this which could explain all you problems.  You give each event a name when you create them.  This will cause problems if you run two instances of the program because both the programs will share the events.  Then if one program sets the event the other one could catch it and reset it.  
This would also explain why SetEvent didn't work or why an event was suttenly set.  You would also have problems if the SYSTEM is using events with the same name.

I therefore suggjest that you do not give you events names when you create them.

There is no way that you will have problems when you call SetEvents in diffrent files.  I do it all the time and it works great.
0
 
LVL 1

Expert Comment

by:gvg
ID: 1324108
Just adding comment because I forgot to ask for an e-mail :-)
0
 
LVL 2

Accepted Solution

by:
wyy_cq earned 100 total points
ID: 1324109
/////////// fatal error on logic
WaitForSingleObject(hEventWaitConf,INFINITE);
WaitForSingleObject(hEventWaitInd,4000);
//////////
if the first wait is not end
SetEvent(hEventWaitInd) will not work.
you may replace the two lines with
WaitForMultiObjects(....);


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

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 …
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
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.
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

932 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

12 Experts available now in Live!

Get 1:1 Help Now