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

后端 未结 4 972
耶瑟儿~
耶瑟儿~ 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:20

    Yes and no; depending on what operating system is present, if any is present at all, and what you are trying to achieve.

    If you have the luxury of a full multi-tasking and multi-threading operating system available, then you must pick your primitives from the collection it provides you, or you risk inefficiencies at best and non-working synchronization at worst. Each OS has its idioms and preferred mechanisms, and failing to follow those conventions can also have costs.

    The further you get from a full kernel (or the deeper into the kernel and device drivers you get), you will find that the best idioms involve lower level synchronization primitives.

    Even a single core CPU has interrupt handlers that can execute (in principle) between any pair of instructions, or even during certain multiple-cycle instructions in some architectures. This is effectively a kind of concurrency, albeit weaker than a second core, so synchronization primitives are required when communicating between the foreground thread(s) and any interrupt handlers in the background. In a single core, synchronization between foreground threads must involve a context switch, of course.

    Waiting on a condition set in an interrupt handler or on a condition set in a hardware register are both cases where a single foreground thread in single core might have no better choice than to spin on the flag or register.

    Edit: I've tried to clarify this answer to make it clear that I'm talking about synchronization in general more than any specific OS's implementation of a spinlock. The question isn't specific about what OS (if any) and isn't tagged for any specific OS either.

提交回复
热议问题