Why slim reader/writer exclusive lock outperformance the shared one?

Posted by Jichao on Stack Overflow See other posts from Stack Overflow or by Jichao
Published on 2012-11-03T05:49:33Z Indexed on 2012/11/04 5:01 UTC
Read the original article Hit count: 307

Filed under:
|

I have tested the performance of slim reader/writer lock under windows 7 using the codefrom Windows Via C/C++.

The result surprised me that the exclusive lock out performance the shared one. Here are the code and the result.

unsigned int __stdcall slim_reader_writer_exclusive(void *arg)
{
    //SRWLOCK srwLock;
    //InitializeSRWLock(&srwLock);

    for (int i = 0; i < 1000000; ++i) {
        AcquireSRWLockExclusive(&srwLock);
        g_value = 0;
        ReleaseSRWLockExclusive(&srwLock);
    }
    _endthreadex(0);
    return 0;
}

unsigned int __stdcall slim_reader_writer_shared(void *arg)
{

    int b;
    for (int i = 0; i < 1000000; ++i) {
        AcquireSRWLockShared(&srwLock);
        //b = g_value;
        g_value = 0;
        ReleaseSRWLockShared(&srwLock);
    }
    _endthreadex(0);
    return 0;
}

g_value is a global int volatile variable.

enter image description here

Could you kindly explain why this could happen?

© Stack Overflow or respective owner

Related posts about Windows

Related posts about multithreading