Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Multithread problem (SetEvent), URGENT!!

Posted on 1998-11-01
3
Medium Priority
?
670 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 200 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses

610 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