Thread: Critical Sections, destroying

  1. #1
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879

    Critical Sections, destroying

    Hi, I'm starting to work with simple threads now, but I'm having trouble figuring out how to delete my critical sections properly. I have 2 threads, one for handling network stuff, and the other for handling the GUI, since the redrawing slows to a crawl when lots of users are connected. My problem is, if the program quits and the main thread deletes the critical section before all the GUI-thread code is done, I might cause the GUI thread to block forever or something as it waits to enter the critical section. Will I need to change all of my GUI thread calls to TryEnterCriticalSection() instead of just enter?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  2. #2
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    You could change the method by which the threads enter the critical section (using TryEnterCriticalSection() rather than EnterCriticalSection()), but that may not be the best solution.

    Instead, you could create an event object that would be signaled when your application closes. Have all threads that access the critical section check the status of the shutdown event object via a call to WaitForSingleObject() with a timeout of zero.

    Now, on application shutdown, the main thread would signal the shutdown event. Desiring to enter the critical section, the network thread queries the status of the shutdown event. The thread realizes that the shutdown event has been signaled, avoids entering the critical section, performs any deinitialization tasks, and returns.

    After the main thread signals the shutdown event, it deletes the critical section, performs any deinitialization tasks, and returns.

    This framework ensures that no thread enters a nonexistent critical section and also creates a shutdown event that could be used to automatically inform other parts of the application that the user wishes to exit.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  3. #3
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, that sounds pretty neat. But what I'm worrying about is what if the network thread enters the critical section, then the main thread terminates and tries to delete the critical section before the network thread leaves it? Or should I have 2 extra global variables, and do something like this:
    Code:
    bool quit = false, networkQuit = false, mainQuit = false;
    
    //network thread
    while(!quit)
    {
         ...
    }
    if(mainQuit)
         DeleteCriticalSection(...);
    networkQuit = true;
    return 0;
    
    //main thread
    while(!quit)
    {
         ...
    }
    if(networkQuit)
         DeleteCriticalSection(...);
    mainQuit = true;
    return 0;
    I guess there's the possibility that the if(mainQuit) and if(networkQuit) would get executed at the same time, but the chances are much slimmer. Would this work?
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

  4. #4
    Registered User johnnie2's Avatar
    Join Date
    Aug 2001
    Posts
    186
    Originally posted by Hunter2
    But what I'm worrying about is what if the network thread enters the critical section, then the main thread terminates and tries to delete the critical section before the network thread leaves it?
    Interestingly, the scenario you've suggested is very unlikely to occur. Windows's multithreading capabilities are based upon a system of thread priorities. Threads of relatively high priorities receive greater processing time and more convenient access to certain resources while threads of relatively low priorities receive fewer time slices and may be forced to wait for certain resources. The operating system automatically increases or decreases the priority of a thread based on its current actions and its relationship with other threads.

    This last requirement induces the operating system to raise the priority of a thread within a critical section; since critical sections may be locked by a single thread at a time, the best thread management approach dictates that the priority of a thread within a critical section should be increased. Raising the thread's priority would entitle the thread to greater amounts of time during which to perform its operations and, ultimately, release the critical section to other waiting threads as soon as possible.

    As a result of this management scheme, the priority of the network thread, which has just entered the critical section, is increased by the operating system. The operating system's thread scheduler allows the network thread to continue uninterrupted by the main thread. Therefore, the main thread is unable to deallocate the critical section; it simply is not given the time to do so.

    When the network thread exits the critical section, its priority is automatically lowered by the operating system and other threads (including the main thread) are allowed to execute. When the main thread is granted a time slice, it proceeds to signal the shutdown event object and safely deallocate the critical section.

    In general, threads within synchronization objects like the critical section are temporarily given increased priorities in order to free the synchronization object for use by other threads.
    "Optimal decisions, once made, do not need to be changed." - Robert Sedgewick, Algorithms in C

  5. #5
    Carnivore ('-'v) Hunter2's Avatar
    Join Date
    May 2002
    Posts
    2,879
    Hmm, never thought of the auto-priority. But I just realized I can simply waitforsingleobject on the network thread's handle to make sure it's shut down before I delete the critical section. Thanks
    Just Google It. √

    (\ /)
    ( . .)
    c(")(") This is bunny. Copy and paste bunny into your signature to help him gain world domination.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Semaphores and critical sections
    By Yasir_Malik in forum Windows Programming
    Replies: 3
    Last Post: 04-01-2006, 11:02 AM
  2. Reading only sections of a text file
    By Queatrix in forum C++ Programming
    Replies: 5
    Last Post: 04-10-2005, 04:19 PM
  3. Critical Updates.
    By anonytmouse in forum Tech Board
    Replies: 4
    Last Post: 09-30-2003, 07:50 AM
  4. When to use Critical Sections in Threads?
    By Aidman in forum Windows Programming
    Replies: 8
    Last Post: 07-20-2003, 05:10 PM