Click here to Skip to main content
15,890,717 members
Articles / Programming Languages / C++

Re-Entrant Custom Critical Section

Rate me:
Please Sign up or sign in to vote.
3.00/5 (5 votes)
14 Nov 2010CPOL2 min read 43.8K   249   13   9
Re-Entrant code

Introduction

The motivation to write this article came from Dr.Sai's excellent article "Working with CRITICAL_SECTION", so before reading this article one should first review that article.

This article explains how we can extend the custom critical section architecture to make it re-entrant.

Detail 

Enter Part

void MyCriticalSection::Enter()

To make it re-entrant we need to have a variable to save the recursion count, m_lRecursionCount. The recursion count is local to the thread, is incremented each time that EnterCriticalSection is called and keeps count of the number of times that EnterCriticalSection is called in the thread.

In the above scenario, the expression if (m_ThreadID == GetCurrentThreadId()) will evalute to TRUE and increment the recursion count. Prior to that, m_threadID is made equal to GetCurrentThreadId() once either interlockedincrement(&count) == 0 or waitforsingleobject succeeds.

Leave Part

void MyCriticalSection::Leave()

The expression if (m_ThreadID != GetCurrentThreadId()) is necessary so that no other thread makes a call to Leave() without making a call to Enter(), in which case the call to Leave should be ignored.

First a check of m_lrecursivecount == 0 is made. If the recursion count is not zero, the recursion count is decremented, which means the thread needs to call Leave some additional times before relinquishing control to other waiting threads.

If the statement m_lrecursivecount == 0 evaluates to TRUE, then we need to decrement the count and make a call to SetEvent. Before making a call to SetEvent we need to check that InterlockedDecrement(&count) > 0. If TRUE, a call to SetEvent is made; if FALSE, it is the last thread and hence there is no need to call SetEvent.

Conclusions

One should step through this program and examine every step until satisfied that it does work.

Finally thanks to Dr.Sai's article for helping me open the petals of knowledge. I am hoping for his comments on this article.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) IBM
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionWin32 CriticalSection is far faster Pin
Xarium29-Mar-12 14:40
Xarium29-Mar-12 14:40 
GeneralWindows via C/C++ (Richter) Pin
FrankLaPiana22-Nov-10 11:46
FrankLaPiana22-Nov-10 11:46 
Recommended reading...

Your "critical section" emulates the Win32/64 critical section although it is has a smaller size. I suspect it's a lot less efficient as well. Have you profiled the CPU/time usage of your lock vs critical_section vs other application-level locks?

Most locking mechanisms have 2 classes; a base locking mechanism and then its scope guard class. A lot of the Windows code I've seen has using the guard paradigm.

// code is scope and exception protected.
{
LockAuto oLock (&LockingBaseInstance)
...
... locked code area
...
}


A lock can be passed to another thread although this is unusual; allowing the secondary thread to unlock would be necessary in this case.

Using an interlocked operation does not guarantee any locking order when there is contention. An interlock itself is expensive (though not as expensive as a critical section) because it must be be synchronized across cores (when there is multiple core access). The variable is "volatile" by definition of the interlocked function; this may or may not require a main-memory -> cache -> core transfer on every test-and-set.
GeneralInteresting re-entrance problem. Pin
Xanblax26-Sep-07 5:20
Xanblax26-Sep-07 5:20 
Question¿Time-out capabilities? Pin
Xanblax24-Sep-07 23:22
Xanblax24-Sep-07 23:22 
GeneralDoes not work! Pin
sreskovi19-Sep-07 4:05
professionalsreskovi19-Sep-07 4:05 
GeneralRe: Does not work! Pin
Xanblax25-Sep-07 22:07
Xanblax25-Sep-07 22:07 
GeneralRe: Does not work! Pin
Bharath NS10-Oct-07 20:50
Bharath NS10-Oct-07 20:50 
GeneralPlease vote my Article Pin
Santhosh G_20-Feb-11 3:40
Santhosh G_20-Feb-11 3:40 
GeneralDr.Sai's Comments Pin
Dr. Sai25-Oct-06 0:30
Dr. Sai25-Oct-06 0:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.