CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Sep 2003
    Posts
    815

    LeaveCriticalSection

    Hi,

    I was wondering what happens if I call LeaveCriticalSection API without calling first EnterCriticalSection

    Another thing does EnterCriticalSection/LeaveCriticalSection throws an exception (C++ exceptions) if needed

    Thanks in advance
    avi123

  2. #2
    Join Date
    May 2005
    Posts
    4,954

    Re: LeaveCriticalSection

    its now ok dont do it, here is what the MSDN says

    "If a thread calls LeaveCriticalSection when it does not have ownership of the specified critical section object, an error occurs that may cause another thread using EnterCriticalSection to wait indefinitely"

    before you EnterCriticalSection () call TryCriticalSection () to watch if you can gain ownership.

    if i helped dont forget to rate :-)

    Cheers

  3. #3
    Join Date
    Sep 2003
    Posts
    815

    Re: LeaveCriticalSection

    Thank Golan, I read it too in the MSDN

    the thing is that I want to put in try catch and I don't know if I get to the catch if the critica section was already enter or not I guess then I can use TryEnterCriticalSection

    Thanks
    Avi123

  4. #4
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: LeaveCriticalSection

    A try/catch won't help you much on an entirly C based API . But __try/__except may help you.
    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  5. #5
    Join Date
    Oct 2002
    Location
    Germany
    Posts
    6,205

    Re: LeaveCriticalSection

    Quote Originally Posted by avi123
    I was wondering what happens if I call LeaveCriticalSection API without calling first EnterCriticalSection
    Why would you want to do this?

    There have to be ways to avoid this situation at all costs.

    Exception handling is meant for 'exceptional' (non-expected) situations. It's not meant to compensate for incorrect programming practices.

    Here the problem has been identified.
    You should attempt fixing it at it's very roots.

  6. #6
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: LeaveCriticalSection

    Quote Originally Posted by avi123
    the thing is that I want to put in try catch and I don't know if I get to the catch if the critica section was already enter or not I guess then I can use TryEnterCriticalSection
    Maybe you can do something like this:
    Code:
    __try
    {
    
        */
            some code that might throw an exception
        */
    
        // enter a nested try-finally for your synchronized code
        __try
        {
            EnterCriticalSection(...);
    
            */
                more code that might throw an exception
            */
        } 
        __ finally
        {
            LeaveCriticalSection(...);
        }
    }
    __except(1)
    {
        // handle exceptions here...
    }
    - petter

  7. #7
    Join Date
    Sep 2003
    Posts
    815

    Re: LeaveCriticalSection

    Thank you all
    How do I know if EnterCriticalSection failed?
    If I want to put it in __try __except what should I except (I mean catch)

    what I mean is

    __try
    {
    EnterCriticalSection(m_myCriticSec)

    }

    __except(/* what should I put in here*/)
    {


    }

    and what can I do if the LeaveCriticalSection fails, Am I stuck I mean the critical section can't be released

    Thanks
    Avi123

  8. #8
    Join Date
    Mar 2004
    Location
    (Upper-) Austria
    Posts
    2,899

    Re: LeaveCriticalSection

    Use code tags when posting code!

    I am not offering technical guidiance via email or IM
    Come on share your photo with us! CG members photo album!
    Use the Code Tags!

  9. #9
    Join Date
    Mar 2005
    Location
    Canada Alberta
    Posts
    80

    Re: LeaveCriticalSection

    http://msdn.microsoft.com/library/de...calsection.asp

    MSDN is your freind ^_^ (sometimes :\)
    In C, you merely shoot yourself in the foot.

    In C++, you accidentally create a dozen instances of yourself and shoot them all in the foot. Providing emergency medical care is impossible, because you can't tell which are bitwise copies and which are just pointing at others and saying, "That's me, over there."

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured