Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / November 2006

Tip: Looking for answers? Try searching our database.

how to check if Monitor::Exit has been called

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ian - 10 Nov 2006 21:39 GMT
Monitor::Enter and Monitor::Exit must be called in pairs.   Is there a way
to determine if Monitor::Exit has been called so that it does not get called
a second time?   The psuedocode below illustrates a situation I would like
to implement.  Essentially there are 2 monitors and the first monitor can be
released in 2 different locations.   This sample codes uses a booleen
variable to make certain the first monitor is not released twice.  My
question is, does .NET offer a better way to check if a monitor object has
already been released/exited?

Thanks,

Ian

Monitor::Enter(  resource1.Object );
Monitor::Enter( resource2.Object );
bool bMonitor1IsActive = true;

try {
   // ...uses resources locked by resource1.Object and resource2.Object ,
may throw exceptions...

   Monitor::Exit( resource1.Object );
   bMonitor1IsActive = false;

   // ...excute code locked to resource2.Object , may throw exceptions...
}
catch( Exception ^pE ) {
   // ... handle exception ...
}
finally {
   if( bMonitor1IsActive )
       Monitor::Exit( resource1.Object );
   Monitor::Exit( resource2.Object );
}
Carl Daniel [VC++ MVP] - 10 Nov 2006 21:59 GMT
> Monitor::Enter and Monitor::Exit must be called in pairs.   Is there a way
> to determine if Monitor::Exit has been called so that it does not get
[quoted text clipped - 4 lines]
> twice.  My question is, does .NET offer a better way to check if a monitor
> object has already been released/exited?

That's exactly what the lock keyword is for

lock (resource2)
{
   lock(resource1)
   {
       // code that needs both
   }

   // code that only needs resource 2
}

-cd
Ian - 10 Nov 2006 22:12 GMT
Hello Carl,

Are you referring to the 'lock' class in the C++ support library?   I just
found it in the online help and will take a look at it.

Thanks,

Ian
Carl Daniel [VC++ MVP] - 10 Nov 2006 22:01 GMT
"Carl Daniel [VC++ MVP]" <cpdaniel_remove_this_and_nospam@mvps.org.nospam>
wrote in message news:...
>> Monitor::Enter and Monitor::Exit must be called in pairs.   Is there a
>> way to determine if Monitor::Exit has been called so that it does not get
[quoted text clipped - 16 lines]
>    // code that only needs resource 2
> }

Sorry - wrong language :)

To accomplish the same thing in C++/CLI, which lacks the lock keyword, you
have to use try/finally blocks to ensure that Monitor::Exit is called
exactly once.

-cd

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.