CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2005
    Posts
    63

    critical section question

    Hi I am using critical section and I have a question. I thought that if a thread locks a critical section, another thread will wait until the first thread unlocks the critical section. It seems that it does not lock the critical section though. Instead in the critical section structure, recursionCount is incremented and the owning thread changes to the id of the thread that called EnterCriticalSection first. I set it up as follows:
    Code:
    CRITICAL_SECTION RREQ_VALID_CRITICAL_SECTION; /*locks rreq_valid_Buffer and backPtr_buffer*/
    
    InitializeCriticalSection(&RREQ_VALID_CRITICAL_SECTION);
    What is that recursionCount part. Can anybody explain
    Anything special that I should have done for it to lock.
    Thanks
    Amish

  2. #2
    Join Date
    Feb 2002
    Posts
    4,640

    Re: critical section question

    Yes, if one thread locks a CS, other threads will wait for the lock to be released, before they can lock it.

    Just initializing the CS is not enough, you must also lock it. You need to call EnterCriticalSection to lock it. The link there also provides an example (don't forget to call "LeaveCriticalSection" when you're done).

    Viggy

  3. #3
    Join Date
    Jan 2005
    Posts
    63

    Re: critical section question

    I do call EnterCriticalSection and LeaveCritical section but the problem described above still happens
    Amish

  4. #4
    Join Date
    Oct 2002
    Location
    Singapore
    Posts
    3,128

    Re: critical section question

    I don't think anyone to guess what's wrong with your code. It will be easier to debug if you can post a small compilable sample of your code that replicates the same problem.
    quoted from C++ Coding Standards:

    KISS (Keep It Simple Software):
    Correct is better than fast. Simple is better than complex. Clear is better than cute. Safe is better than insecure.

    Avoid magic number:
    Programming isn't magic, so don't incant it.

  5. #5
    Join Date
    Sep 2004
    Posts
    519

    Re: critical section question

    Note that critical sections don't protect against reentrancy ie the same thread can lock a critical section several times without locking.

    Common scenarios where reentrancy can happen is if you do a windows application, a STA COM object or uses events/callbacks.

    In order to check if this indeed is the problem you can investigate the call-stack when it locks the critical section. See if the critical section been locked earlier in the call-stack.

    Hope this helps

  6. #6
    Join Date
    Feb 2005
    Location
    Normandy in France
    Posts
    4,590

    Re: critical section question

    Quote Originally Posted by axr0248
    Anything special that I should have done for it to lock.
    A thread blocks when:
    • It calls EnterCriticalSection.
    • AND *another* thread owns the *same* critical section object, by having called EnterCriticalSection *successfully* and not yet released it with a *successful* LeaveCriticalSection.
    • AND both the owner thread and the current thread must be in the same process which is the process in which InitializeCriticalSection has been called.
    • AND it blocks only the current thread, and only until the owner thread calls LeaveCriticalSection the same number of times it had called EnterCriticalSection (once, most of the time); there might be a small delay (often several milliseconds), though you can't rely on this delay, which could be from 0 nanoseconds to several years!
    • AND, of course, the critical section must have been correctly initialized, and not yet destroyed (like any other resource : you must only use it when it is alive).

    There are perhaps other conditions : Check for each of these condition, in order to track down the bug.
    Last edited by SuperKoko; June 27th, 2006 at 03:13 AM.
    "inherit to be reused by code that uses the base class, not to reuse base class code", Sutter and Alexandrescu, C++ Coding Standards.
    Club of lovers of the C++ typecasts cute syntax: Only recorded member.

    Out of memory happens! Handle it properly!
    Say no to g_new()!

  7. #7
    Join Date
    Jan 2005
    Posts
    63

    Re: critical section question

    I think marten range was right on the money there. It must be that one of my threads is calling EnterCriticial section multiple times without calling LeaveCriticalSection. Alas posting some simple code that reproduce the problem is really hard because my code has 20 different threads with as many criticial sections and it's hard to trace down to which thread is doing it. Thanks a lot for the info about when a critical section is locked. I go over my code more in depth. Hopefully i can fix it. Thanks
    Amish

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