This API name is long enough. I like it! In fact, the critical_section operation is very lightweight. Why should I add the rotation lock? In fact, this function does not work on a single-CPU computer. It only works when there is more than one CPU on the computer, or a single CPU but multiple cores. If the operation that the critical section is used to protect takes a short time, for example, to protect a reference
Counter, or a flag, then several clock cycles will leave the critical section. However, before this thread leaves the critical section, another thread tries
This critical section -- this happens only on a multi-core or SMP system -- if the thread cannot be accessed, the thread goes to sleep, and then a context switch occurs. We know
Context
Switch is a time-consuming operation. It is said that it requires thousands of clock cycles. In fact, we only need to wait a few more clock cycles to enter the critical section, now there are thousands more clock cycle overhead.
Yes. Therefore, the initializecriticalsectionandspincount function is introduced. Its first parameter is the pointer to CS, and the second parameter.
Is the number of times of rotation. In my understanding, the number of loops, such as N, means that the entercriticalsection () function will cyclically determine whether the critical section can be entered.
Until it can be entered or N times full. The overhead we increase is a maximum of N cycles, and we may receive thousands of clock cycles as a bonus. For very short operations in the critical section, the advantage of doing so is great. On msdn, they used the n = 4000 rotation lock for the heap manager, and then "This gives great performance and scalability in almost all worst-case scenarios. "visible or useful :-) http://yjl.spaces.live.com/blog/cns! 8129c4219a8e995b! 466. Entry