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

前端 未结 4 1750
小鲜肉
小鲜肉 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:04

    The safest method with a performance boost is a hybrid of the two: an adaptive mutex.

    When your system has multiple cores you spin for a few thousand cycles to capture the best case of low or no contention, then defer to a full mutex to yield to other threads for long contended locks.

    Both POSIX (PTHREAD_MUTEX_ADAPTIVE_NP) and Win32 (SetCriticalSectionSpinCount) have adaptive mutexes, many platforms don't have a POSIX spinlock API.

提交回复
热议问题