When is pthread_spin_lock the right thing to use (over e.g. a pthread mutex)?

前端 未结 4 1754
小鲜肉
小鲜肉 2020-12-04 14:35

Given that pthread_spin_lock is available, when would I use it, and when should one not use them ?

i.e. how would I decide to protect some shared data structure with

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-04 15:02

    The spinlock is a "busy waiting" lock. It's main advantage is that it keeps the thread active and won't cause a context switch, so if you know that you will only be waiting for a very short time (because your critical operation is very quick), then this may give better performance than a mutex. Conversely a mutex will cause less demand on the system if the critical section takes a long time and a context switch is desirable.

    TL;DR: It depends.

提交回复
热议问题