Solved

Multithread problem (SetEvent), URGENT!!

Posted on 1998-11-01
3
645 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
Comment Utility
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
Comment Utility
Just adding comment because I forgot to ask for an e-mail :-)
0
 
LVL 2

Accepted Solution

by:
wyy_cq earned 100 total points
Comment Utility
/////////// 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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

7 Experts available now in Live!

Get 1:1 Help Now