Re: A new Critical Section for high contention situations



The state of the lock variable can change immediately after you call InterlockedCompareExchange.

Sleep(0) accomplishes nothing, and is not even guaranteed to sleep at all.

I suggest that it is up to you to explain why and how you think this works.


"Kürsat" <xx@xxxxxx> wrote in message news:O6WqZyqtIHA.3604@xxxxxxxxxxxxxxxxxxxxxxx
Keith,
Thank you for your comments. I am not agree with you about your claims below :

You said "there is no synchroniztion", then, what does synchronization mean? AFAIK, synchronization means "accepting only one thread to access certain resource or functionality at a time". My object achive this, so, I think, there is synchronization.

You said "there is no contention", then what does contention mean? AFAIK, contention (in the current context) means "more than one thtread to attemp to access certain resource or functionality at the same time". While using my object, this condition is highly possible, so "there is contention".

You mentioned "race conditions" but, race conditions occurs on resources not on synchronization objects.

Please correct me if I am in the wrong. I don't claim that this short implementation is correct but your arguments are contradictory with my knowledge.


"Keith Moore" <keithmo@xxxxxxxxxx> wrote in message news:eTMyk0ptIHA.4260@xxxxxxxxxxxxxxxxxxxxxxx
Kürþat wrote:
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);
}
};

There's no contention with this lock because... uhh... there's no synchronization. If enter() is called on an already-locked object, the calling thread will call Sleep(0) *once* then return. Race conditions galore will ensue.

KM




--
Scott McPhillips [VC++ MVP]

.



Relevant Pages

  • 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: What happened to computer architecture (and comp.arch?)
    ... synchronization models at lest BigO(n**2) in time? ... It is _easy_ to provide sync when contention is zero, it is _hard_ to do the same when lots of cores/threads tries to do it simultaneously. ... Each thread could use a hash of its thread ID to determine which first-level lock to use. ...
    (comp.arch)
  • Re: A new Critical Section for high contention situations
    ... You said "there is no synchroniztion", then, what does synchronization mean? ... You said "there is no contention", ... to access certain resource or functionality at the same time". ... You mentioned "race conditions" but, race conditions occurs on resources not ...
    (microsoft.public.win32.programmer.kernel)
  • Re: A new Critical Section for high contention situations
    ... synchronization means "accepting only one thread to access certain resource or functionality at a time". ... You said "there is no contention", ... You mentioned "race conditions" but, race conditions occurs on resources not on synchronization objects. ... At this point, Thread A owns the lock, but Thread B "thinks" it owns the lock. ...
    (microsoft.public.win32.programmer.kernel)
  • Re: A new Critical Section for high contention situations
    ... You said "there is no synchroniztion", then, what does synchronization mean? ... resource or functionality at a time". ... the resulting app will be CPU-heavy if there's a lot of contention for a ... What do you despise? ...
    (microsoft.public.win32.programmer.kernel)