Is spin lock useful in a single processor uni core architecture?

后端 未结 4 953
耶瑟儿~
耶瑟儿~ 2021-01-31 11:55

I am confused by the function of spin lock.

The spin lock is used to stop the process from re-scheduling. However, in a machine with just one core, is it useful to use s

4条回答
  •  抹茶落季
    2021-01-31 12:16

    Short answer: no.

    According to http://uw714doc.sco.com/en/man/html.3synch/Intro.3synch.html

    Spin locks must not be used on a single processor system. In the best case, a spin lock on a single processor system will waste resources, slowing down the owner of the lock; in the worst case, it will deadlock the processor.

    From: http://blogs.microsoft.co.il/blogs/sasha/archive/2008/08/10/practical-concurrency-patterns-spinlock.aspx

    On single-processor systems, spinlocks are not needed because spinlock synchronization is required on high IRQLs only. On high IRQLs (above dispatch IRQL) a context switch cannot occur, so instead of spinning the acquiring thread can simply request an interrupt on the relevant IRQL and return; the interrupt will be masked until the releasing thread lowers the IRQL below the requested IRQL.

    For single processor systems, the kernel will ignore the spin count value, and treat it as zero - essentially making a spinlock a no-op.

    Yes, spin locks can be useful, and improve efficiency of some operations. However, generally you should start with a mutex, and if profiling show it to be a bottleneck, you may want to consider a spinlock.

提交回复
热议问题