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

前端 未结 3 753
遥遥无期
遥遥无期 2021-02-04 15:49

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 o

3条回答
  •  失恋的感觉
    2021-02-04 16:10

    A Windows kernel developer devoted to optimizing locks in Windows told me with regards to performance as a rule of thumb:

    1. Use critical sections for most cases for best performance
    2. Consider SRW locks if you are reading at least 4:1 vs writing
    3. Only use mutexes if your code can absolutely not permit starvation

    Obviously there are other aspects about the locks that one should consider:

    1. you have to clean up a CS whereas you don't have to clean up a SRW lock
    2. CS locks can be taken recursively whereas SRW locks cannot
    3. mutexes can synchronize UM and KM, and if you are in KM, CS and SRW locks aren't really an option

    So yeah, just favor CS unless reads >>>> writes.

提交回复
热议问题