🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Need help with Mutex

#2061
From: SubZero
Date: Sat, 28 Apr 2007 16:34
114 lines
3324 bytes
Hello,

I used to have an application with the following functions for thread 
sync'in.

void inline __fastcall lockCriticalSection(TCriticalSection 
*criticalSection)
{
  criticalSection->Acquire();
};

void inline __fastcall releaseCriticalSection(TCriticalSection 
*criticalSection)
{
  criticalSection->Release();
};


To detect a deadlock, I implemented the below:

void inline __fastcall lockCriticalSection(TCriticalSection 
*criticalSection)
{
  HANDLE hMutex;

  // Create a mutex with no initial owner.

  hMutex = CreateMutex(
   NULL,                       // no security attributes
   true,                      // initially owned
   String((int)(void*)criticalSection).c_str());  // name of mutex

  DWORD dwWaitResult;

  // Request ownership of mutex.

  if(hMutex)
  {
   dwWaitResult = WaitForSingleObject(
   hMutex,   // handle to mutex
   5000L);   // five-second time-out interval

   switch(dwWaitResult)
   {
    // Cannot get mutex ownership due to time-out.
    case WAIT_TIMEOUT:
     throw Exception("BUG FOUND, CHECK CALL STACK!");

    // Got ownership of the abandoned mutex object.
    case WAIT_ABANDONED:
     break;
   }
  }

  criticalSection->Acquire();
};

void inline __fastcall releaseCriticalSection(TCriticalSection 
*criticalSection)
{
  criticalSection->Release();

  HANDLE hMutex;

  // Create a mutex with no initial owner.

  hMutex = OpenMutex(
    MUTEX_ALL_ACCESS,
    false,
    String((int)(void*)criticalSection).c_str()
  );  // name of mutex

  if(hMutex)
  {
   DWORD dwWaitResult;

   // Request ownership of mutex.

   dwWaitResult = WaitForSingleObject(
    hMutex,   // handle to mutex
    5000L);   // five-second time-out interval

   switch(dwWaitResult)
   {
    // The thread got mutex ownership.
    case WAIT_OBJECT_0:
     // Release ownership of the mutex object.
     if(!ReleaseMutex(hMutex))
     {
      // Deal with error.
     }

     break;

    // Cannot get mutex ownership due to time-out.
    case WAIT_TIMEOUT:
     break;

    // Got ownership of the abandoned mutex object.
    case WAIT_ABANDONED:
     break;
   }
  }
};

which does not work for no obvious reason. It simply raises the exception 
when there is no reason! The Critical section variable is -1, 0 at that 
time!

Please help!

Best Regards,

SZ 
Technical Details
Message-ID: <4633777f$1@newsgroups.borland.com>
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail

Show Full Headers
Complete Article Headers
X-Google-Thread: 10cc67,28184df4065864d1,start
X-Google-Attributes: gid10cc67,public
X-Google-Language: ENGLISH,ASCII-7-bit
From: "SubZero" <g_ates@hotmail.com>
Newsgroups: borland.public.cppbuilder.nativeapi
Subject: Need help with Mutex
Date: Sat, 28 Apr 2007 19:34:01 +0300
Lines: 115
MIME-Version: 1.0
Content-Type: text/plain;
 format=flowed;
 charset="iso-8859-9";
 reply-type=original
Content-Transfer-Encoding: 7bit
X-Priority: 3
X-MSMail-Priority: Normal
X-Newsreader: Microsoft Windows Mail 6.0.6000.16386
X-MimeOLE: Produced By Microsoft MimeOLE V6.0.6000.16386
NNTP-Posting-Host: 85.99.193.41
Message-ID: <4633777f$1@newsgroups.borland.com>
X-Trace: newsgroups.borland.com 1177778047 85.99.193.41 (28 Apr 2007 09:34:07 -0700)
Path: arc1-ca.pugleaf.net!g2news1.google.com!news2.google.com!news.glorb.com!solnet.ch!solnet.ch!newsfeed01.sul.t-online.de!t-online.de!newsgroups.borland.com!not-for-mail