A new Critical Section for high contention situations



Hi,

"CRITICAL_SECTION"s are ideal inter-thread syncronization solutions for
low-contention situations but as the contention increases the performance
falls down accordingly because of kernel transition. So developed a simple
syncronization object which always remains in user mode (at least I think
so). I tested both the CRITICAL_SECTION and my object with a high contention
schenario and I see my object gives ~%45 better results (with a dual core
CPU). Wow, cool! What do you think about my implementation? Is there any
obstacle to use it in a commercial software?

(I am not sure about originality of the implementation, it may be
implemented many times by many developers.)

Thanks in advance.

/////////////////////////////////////////////////////////////////////

#define OWNED 1
#define NOT_OWNED 0

class CRITICAL
{
private:
int m_nLock;

public:
CRITICAL () : m_nLock (NOT_OWNED) {}

void enter ()
{
if (InterlockedCompareExchange ((LONG * ) &m_nLock, OWNED,
NOT_OWNED) == OWNED)
{
Sleep (0);
}
}

void leave ()
{
InterlockedExchange ((LONG * ) &m_nLock, NOT_OWNED);
}
};


.



Relevant Pages

  • Re: A new Critical Section for high contention situations
    ... "CRITICAL_SECTION"s are ideal inter-thread syncronization solutions for low-contention situations but as the contention increases the performance falls down accordingly because of kernel transition. ... (I am not sure about originality of the implementation, it may be implemented many times by many developers.) ... void enter ...
    (microsoft.public.win32.programmer.kernel)
  • Re: A new Critical Section for high contention situations
    ... void enter ... You said "there is no synchroniztion", then, what does synchronization ... You said "there is no contention", ... You mentioned "race conditions" but, ...
    (microsoft.public.win32.programmer.kernel)
  • Re: Control in User Control not instantiating
    ... My code has been review by ... other developers and nothing was glarringly wrong with the code (at least to ... protected override void OnPreRender ...
    (microsoft.public.dotnet.framework.aspnet.buildingcontrols)