Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8643983
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:11:03+00:00 2026-06-12T12:11:03+00:00

I am hunting a possible deadlock in my program and im suspecting the following.

  • 0

I am hunting a possible deadlock in my program and im suspecting the following.
What happen if 2 threads at the same time calls EnterCriticalSection and thread #1 calls DeleteCriticalSection right after entering, what happen to thread #2 which is still in EnterCriticalSection call?

Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T12:11:04+00:00Added an answer on June 12, 2026 at 12:11 pm

    What happen if 2 threads at the same time calls EnterCriticalSection
    and thread #1 calls DeleteCriticalSection right after entering, what
    happen to thread #2 which is still in EnterCriticalSection call?

    Two threads cannot enter the critical section at the same time. That would defeat the purpose of a critical section. Either Thread #1 enters the critical section first, or Thread #2 enters the critical section first. You have two possible interleavings at play here.

    Let’s say that the interleaving is this:

        Thread 1               Thread 2
        --------               --------
           |                      |
           |                      |
        EnterCS()                 |
        Lock Taken                |
           |                      |
           |                   EnterCS()
           |                   Blocked
           |                      |
           |                      |
        DeleteCS()                |
           |                      |
           |                     ???
           |
          ...
    
    

    In this case, the state of thread #2 is undefined according to MSDN:

    DeleteCriticalSection function

    Remarks

    Deleting a critical section object releases all system
    resources used by the object.

    After a critical section object has been deleted, do not reference the
    object in any function that operates on critical sections (such as
    EnterCriticalSection, TryEnterCriticalSection, and
    LeaveCriticalSection) other than InitializeCriticalSection and
    InitializeCriticalSectionAndSpinCount. If you attempt to do so, memory
    corruption and other unexpected errors can occur.

    If a critical section is deleted while it is still owned, the state of the threads
    waiting for ownership of the deleted critical section is undefined.

    So if you’re unlucky enough for your two threads to encounter the above interleaving, then the operating system gives you no guarantee that your program will continue to work as expected. That can include deadlocking Thread #2, for example.

    But if the interleaving is this:

        Thread 1               Thread 2
        --------               --------
           |                      |
           |                      |
           |                   EnterCS()
           |                   Lock Taken
           |                      |
        EnterCS()                 |
        Blocked                   |
           |                      |
           |                      |
           |                   ExitCS()
           |                   Lock Released
           |                      |
        Unblocked                 |
        LockTaken                 |
           |                      |
        DeleteCS()                |
           |                      |
           |                      |
          ...                    ...
    

    Then obviously, since Thread #1 is blocked, it can’t delete the critical section, until Thread #2 leaves the critical section. Then assuming no other threads enter the critical section, Thread #1 will be able to delete it with no problems.

    The scenario that you propose is essentially a race condition. Depending on the timing of the threads, it may work just fine or cause unpredictable problems. In this case, you have to restructure your code such that the destruction of the critical section happens after all interested threads have released the critical section.

    In this two-thread scenario, one way to fix it is to have Thread #1 leave the critical section and wait for all the other threads to finish first before deleting the critical section. Something like this, for example:

    // Pseudocode for exposition
    void Thread1()
    {
        EnterCS();
        // Do stuff
        ExitCS();
        WaitForThread2();
        DeleteCS();
    }
    
    void Thread2()
    {
        EnterCS();
        // Do stuff
        ExitCS();
    }
    

    Now, the two possible interleavings look like this:

        Thread #2 acquires lock first:  .  Thread #1 acquires lock first:
                                        .
        Thread 1        Thread 2        .   Thread 1        Thread 2
        --------        --------        .   --------        --------
           |               |            .      |               | 
           |            EnterCS()       .   EnterCS()          |
           |            Lock Taken      .   Lock Taken         |
           |               |            .      |               |
        EnterCS()          |            .  // Do stuff      EnterCS()          
        Blocked        // Do stuff      .      |            Blocked
           |               |            .      |               |
           |               |            .   ExitCS()           |
           |            ExitCS()        .   Lock Released      |
           |            Lock Released   .      |               |
           |               |            .      |            Unblocked
        Unblocked          |            .      |            Lock Taken
        Lock Taken         |            .      |               |
           |               |            .      |           // Do stuff
       // Do stuff         |            .      |               |
           |               |            .      |            ExitCs()
        ExitCS()           |            .      |            Lock Released
        Lock Released      |            .      |               |
           |               |            .      |               |
           |               |            .      |               |
        WaitForThread2() --+            .   WaitForThread2() --+
           |                            .      |
        DeleteCS()                      .   DeleteCS()
           |                            .      |
           |                            .      |
          done                          .     done
    

    The exact implementation for WaitForThread2() is going to depend on the nature of your program, but would certainly involve WaitForSingleObject() or any one of its relatives.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've been hunting for a long time for this, but despite my intuition that
Possible Duplicate: What is the use of the := syntax? I've tried hunting down
Surely this is possible? I have been hunting through PyQt tutorials and documentation but
I have been hunting high and low for a tool to convert an SVG
I'm hunting for a short example on IL generation programming that includes: a return
I'm Hunting the Wumpus, and I've run into an issue. (I'm a high-schooler) I'm
Lately I've been job hunting, and for the most part, they would ask me
I made 5 commits to Master branch when bug hunting on a private project
Environment: VS2005 C++ using STLPort 5.1.4. Compiling the following code snippet: std::string copied =
Hunting internationalization bugs here. Does mysql has a variable which can be set per

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.