Click here to Skip to main content
Licence CPOL
First Posted 14 Oct 2008
Views 7,813
Downloads 66
Bookmarked 9 times

Critical Section Block

By | 14 Oct 2008 | Article
Helper Class for using CRITICAL_SECTION

Introduction

More threads make more critical sections usually. For that, in Windows programming, we use CRITICAL_SECTION variable and related functions. Critical Section Block increases code-readability and prevents errors that could possibly happen.

Background

The simplest way protecting critical section is just using CRITICAL_SECTION variables with EnterCriticalSection() and LeaveCriticalSection() on that area. But it is tedious work and even has the risk of deadlock if we do not really care. So, some of us write an auto_ptr-like class.

class AutoCriticalSection
{
public:
    AutoCriticalSection(CRITICAL_SECTION* pCS)
    : m_pCS(pCS)
    {
        EnterCriticalSection(m_pCS);
    }
    
    ~AutoCriticalSection()
    {
        LeaveCriticalSection(m_pCS);
    }
    
private:
    CRITICAL_SECTION* m_pCS;
}; 

AutoCriticalSection class is worth using. But, think of this. If the critical section is pretty small and it's needed to it make narrow, we should write a new block like the one shown below:

// Now you limit protected range as you want.
{
	AutoCriticalSection(&cs) myAutoCS;
	// Manipulate some shared data here.
}

I thought the code is pretty ugly and decided to use another method. Its result is Critical Section Block.

class CriticalSectionContainer
{
public:
    CriticalSectionContainer(CRITICAL_SECTION* pCS)
    : m_pCS(pCS)
    {
        EnterCriticalSection(m_pCS);
    }
    
    ~CriticalSectionContainer()
    {
            LeaveCriticalSection(m_pCS);
    }
    
    operator bool()
    {
        return true;
    }
    
private:
    CRITICAL_SECTION* m_pCS;
};
    
#define CSBLOCK(x) if (CriticalSectionContainer __csc = x)

Critical parts of Critical Section Block are operator bool() and define using if-statement. It’s very simple code but also very useful.

Using the Code

It’s really simple using it!

CSBLOCK(&cs)
{
	// Manipulate some shared data here.
}

It's also easy to understand.

History

  • 14th October, 2008: Initial post

License

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

About the Author

hongseok.com

Software Developer

Korea (Republic Of) Korea (Republic Of)

Member



Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralAnother Option PinmemberRick York11:44 15 Oct '08  

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

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

Permalink | Advertise | Privacy | Mobile
Web03 | 2.5.120517.1 | Last Updated 14 Oct 2008
Article Copyright 2008 by hongseok.com
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid