🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.cppbuilder.nativeapi
Article #8988

Re: Wait for "signal" from another application and/or thread

#8988
From: zoom
Date: Sat, 14 Aug 2004 06:07
144 lines
6287 bytes
Please copy/paste the following code and observe how  CreateEvent and
WaitForSingleObject works.

The code creates three threads (Th1,Th2,Th3) of type TMyThread.
Threads wait for main application to set the event.
Threads run at different speed (they sleep random amout of time)
Since bManualReset is FALSE on CreateEvent(
NULL,FALSE,FALSE,"MyEventObject") , events reset automatically.

Button1 must be pressed three times to let three object execute their loop.
BUT if Button1 pressed quickly, then threads may
miss some of the set events, because they run at random speeds. If you run
multiple instances of the same application, then pressing
the Button1 on any instance can randomly activate a thread on any instance.

How we can modify this code so that each Button1 press on any instance
activates ALL threads on ALL instances ?

In another word if we run 4 instances of this program we will have 12
threads (each app has 3 threads) and when we
press the Button1 on any of 4 instances all threads will increment their
count by one regardless how quickly we press the Button1.


TForm1 *Form1;
//==========================================================================
=
class TMyThread : public TThread
{
private:
  void __fastcall SyncFunc(void);
  void __fastcall Execute();
  int FCount,FID;
  HANDLE Fhandle;
public:
  __fastcall TMyThread(bool CreateSuspended,int ID);
};
__fastcall TMyThread::TMyThread(bool CreateSuspended,int
ID):TThread(CreateSuspended)
{
  FID=ID;
  FCount=0;
  Fhandle = CreateEvent( NULL,FALSE,FALSE,"MyEventObject") ;
}

void __fastcall TMyThread::SyncFunc(void)
{
  switch (FID) {
    case 1 : Form1->lblThread1Count->Caption =  IntToStr(FCount); break;
    case 2 : Form1->lblThread2Count->Caption =  IntToStr(FCount); break;
    case 3 : Form1->lblThread3Count->Caption =  IntToStr(FCount); break;
  };
  Form1->Memo1->Lines->Add(String().sprintf("%d %d
%s",FCount,FID,TimeToStr(Now()).c_str()));
}

void __fastcall TMyThread::Execute()
{
  while (!Terminated){
    Sleep(rand() % 1000); // Run your threads at different speeds
    BOOL WaitResult = (WaitForSingleObject(Fhandle,INFINITE) ==
WAIT_OBJECT_0);
    //if (WaitResult)   ResetEvent(Fhandle);
    FCount++;
    Synchronize(SyncFunc);
  }
  CloseHandle(Fhandle);
}
//==========================================================================
=
TMyThread *Th1, *Th2, *Th3;
int MySetEventCount = 0;
HANDLE h1;
//--------------------------------------------------------------------------
-
__fastcall TForm1::TForm1(TComponent* Owner)
 : TForm(Owner)
{
  MySetEventCount = 0;
  h1 = CreateEvent( NULL,FALSE,FALSE,"MyEventObject") ;
  Th1 = new TMyThread(false,1);
  Th2 = new TMyThread(false,2);
  Th3 = new TMyThread(false,3);
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::Button1Click(TObject *Sender)
{
   SetEvent(h1);
   MySetEventCount++;
   lblSetEventCount->Caption = MySetEventCount;
}
//--------------------------------------------------------------------------
-
void __fastcall TForm1::FormDestroy(TObject *Sender)
{
   Th1->Terminate();   Th2->Terminate();   Th3->Terminate();
   SetEvent(h1); SetEvent(h1); SetEvent(h1);
   SetEvent(h1); SetEvent(h1); SetEvent(h1);
   delete Th1; delete Th2; delete Th3;
   CloseHandle(h1);
}
//--------------------------------------------------------------------------
-

"Bob Gonder" <notbg@notmindspring.invalid> wrote in message
news:pboqh013avflntd8vgj1vcbrg000u94vb7@4ax.com...
> Eugene Mayevski [SecureBlackbox] wrote:
>
> >Bob Gonder wrote:
> >
> >>>How to make sure that each and every one of the waiting thread be
signalled?
> >> You already create the event to not reset, so if you don't reset the
> >> event in your thread code, then all threads will get the event signal.
> >
> >If the event is auto-reset, they will get it either anyway. The
> >difference between semaphore and event is that semaphore awakes one
> >listener and decrements semaphore by 1. And event awakes all listeners
> >and then is reset. So the OP should use semaphores.
>
> Did I read this wrong?
> "CreateEvent()
> bManualReset
> Specifies whether a manual-reset or auto-reset event object is
> created. If TRUE, then you must use the ResetEvent function to
> manually reset the state to nonsignaled. If FALSE, Windows
> automatically resets the state to nonsignaled after a single waiting
> thread has been released. "
>
> Sounds like, if TRUE, all threads (and the OP had several waiting on
> the same event) will signal, and if FALSE, only One will signal.
> Either that, or the Docs are wrong.
>
> Ahh, perhaps you were recommending Named Semaphores so that all
> threads in all instances of the program could all release with a
> single call?
> For that, I suppose he'd need to use WaitMultiple, and the Named
> Semaphore would be essentially a Kill Notification (Thread gets it and
> self-terminates)  Using the current process-level events for rerunning
> the threads. (Or a second local semaphore so he could restart a given
> number of threads with one call.)
>
>
Technical Details
Message-ID: <411dac31$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail
References: <4105617c$1@newsgroups.borland.com> <4105777f$3@newsgroups.borland.com> <411ccf89@newsgroups.borland.com> <gnsph0pq4c8mfhapah6cideqmbp6gqor5r@4ax.com> <411d2245@newsgroups.borland.com> <pboqh013avflntd8vgj1vcbrg000u94vb7@4ax.com>

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,b6552239fe09c315
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: <zoom>
Newsgroups: borland.public.cppbuilder.nativeapi
References: <4105617c$1@newsgroups.borland.com> <4105777f$3@newsgroups.borland.com> <411ccf89@newsgroups.borland.com> <gnsph0pq4c8mfhapah6cideqmbp6gqor5r@4ax.com> <411d2245@newsgroups.borland.com> <pboqh013avflntd8vgj1vcbrg000u94vb7@4ax.com>
Subject: Re: Wait for "signal" from another application and/or thread 
Date: Sat, 14 Aug 2004 16:07:45 +1000
Lines: 146
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Outlook Express 6.00.2800.1437
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2800.1441
NNTP-Posting-Host: 211.30.68.31
Message-ID: <411dac31$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1092463665 211.30.68.31 (13 Aug 2004 23:07:45 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.icl.net!newsfeed.freenet.de!newsfeed00.sul.t-online.de!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail