🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Mutex and program instancenumbers

#15894
From: Hans Galema
Date: Fri, 06 Jun 2003 15:24
92 lines
3553 bytes
For testing a client/server application I start many instances of the client
on the same computer. I want the clients to know which instancenumber they have.
At startup a client tries to open a mutex with mutexname 'client1'. If the mutex
cannot be openend it is the first client and this client creates the mutex and
gets instancenr 1. The next client will find that mutexname 'client1' can be
openend and tries 'client2' and so on. This works ok.

If I start four clients (1-4) and then close clients 2 and 3, I would expect
that a next started client would get nr 2 again. Instead it gets nr 5. Always
the next number of the highest number present will be found.

Why is this so ?

All the code is below.

Hans.


class TForm1 : public TForm
{
__published:	// IDE-managed Components
   TMemo *Memo1;
private:	// User declarations
  HANDLE mutex;
  int instancenr;

  int __fastcall DetermineInstancenr(void);
public:		// User declarations
   __fastcall TForm1(TComponent* Owner);
   __fastcall ~TForm1(void);

};

int __fastcall TForm1::DetermineInstancenr ( void )
   {
   const int MAXINSTANCES = 6;

   int nr = 0;

   while ( ++ nr <= MAXINSTANCES )
       {
       AnsiString MutexName = Application->Title + IntToStr ( nr );

       mutex = OpenMutex (MUTEX_ALL_ACCESS, false, MutexName.c_str() );

       if (mutex == NULL)
       	{
       	// The mutex is not there. This means another
         // instance of the program with this instancenr
       	// is not already running. Create the mutex so any
         // other instance of the program will know that
         // this one is already running.

       	mutex = CreateMutex (NULL, true, MutexName.c_str()) ;

         instancenr = nr;

         break;
     	}
      }

   if ( nr > MAXINSTANCES )
     {
     ShowMessage ( Application->Name + "(1-" + IntToStr (MAXINSTANCES)
                    + ")\n\nalready running." ) ;

     return 0 ;
     }

   return instancenr;
   }

//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
  DetermineInstancenr ();

  Caption = Application->Title + " " + IntToStr ( instancenr );

  /*
   Make a small TForm and place on Top,Right of Screen.
   Every next instance will shift to Bottom,Left
  */
  Top  += ( instancenr - 1 ) * 25;
  Left -= ( instancenr - 1 ) * 25;
}
//---------------------------------------------------------------------------
  __fastcall TForm1::~TForm1(void)
{
   if ( mutex ) ReleaseMutex (mutex) ;
}
Technical Details
Message-ID: <3ee0b2cd@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,aeacbe3cdfad6f92,start
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-06-06 08:36:45 PST
Date: Fri, 06 Jun 2003 17:24:56 +0200
From: Hans Galema <j.m.galema.dontusethis@maartens.nl>
User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.3.1) Gecko/20030425
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Mutex and program instancenumbers
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 129.125.108.11
Message-ID: <3ee0b2cd@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1054913229 129.125.108.11 (6 Jun 2003 08:27:09 -0700)
Lines: 93
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-06!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail