🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Mutex and program instancenumbers

#15898
From: Eudy Carvalhaes C. Silva
Date: Fri, 06 Jun 2003 16:47
118 lines
4216 bytes
You have to CloseHandle(mutex) in case
the OpenMutex is successful, otherwise
there will be an open handle to the
mutex.

If you're running 4 instances, instance
3 and 4 both have open handles to mutex
2. When instance 2 closes it ReleasesMutex 2,
but "The mutex object is destroyed when its last
handle has been closed" -> last remark in OpenMutex
in the online help.

Eudy


Hans Galema wrote:
> 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: <3EE0C5BA.1040407@iae.cta>
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail
References: <3ee0b2cd@newsgroups.borland.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 10cc67,aeacbe3cdfad6f92
X-Google-Attributes: gid10cc67,public
X-Google-ArrivalTime: 2003-06-06 09:56:59 PST
Message-ID: <3EE0C5BA.1040407@iae.cta>
Date: Fri, 06 Jun 2003 13:47:54 -0300
From: "Eudy Carvalhaes C. Silva" <eudy@iae.cta>
User-Agent: Mozilla/5.0 (Windows; U; Win98; en-US; rv:1.0.2) Gecko/20021120 Netscape/7.01
X-Accept-Language: en-us, en
MIME-Version: 1.0
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Re: Mutex and program instancenumbers
References: <3ee0b2cd@newsgroups.borland.com>
Content-Type: text/plain; charset=us-ascii; format=flowed
Content-Transfer-Encoding: 7bit
NNTP-Posting-Host: 161.24.47.98
X-Trace: newsgroups.borland.com 1054917940 161.24.47.98 (6 Jun 2003 09:45:40 -0700)
Lines: 119
Path: arc1-ca.pugleaf.net!archiver1.google.com!news1.google.com!sn-xit-02!sn-xit-04!sn-xit-01!sn-xit-09!supernews.com!newsfeed.wirehub.nl!news.kweb.on.ca!KWeb!newsgroups.borland.com!not-for-mail