Solved

Multithread problem (SetEvent), URGENT!!

Posted on 1998-11-01
3
651 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
[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
  • 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Installing Microsoft R server on REDHAT or SUSE Linux 4 152
Best tools for Backup and restore of Wordpress site 6 58
bigDiff challenge 17 134
Line meaning 9 90
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

749 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