?
Solved

Multithread problem (SetEvent), URGENT!!

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

Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

Question has a verified solution.

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

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…
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.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

770 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