Why slim reader/writer exclusive lock outperformance the shared one?
- by Jichao
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.
Could you kindly explain why this could happen?