3 comments

  • tombert2 hours ago
    I genuinely had not heard of anyone actually using a spinlock in production code until I started using LMAX Disruptor a few years ago.<p>I was always told that they were an anti-pattern, and I think that generally that is a pretty good rule of thumb, but I guess like most stuff in CS: there are always exceptions to &quot;good rules of thumb&quot;.<p>I still haven&#x27;t actually explicitly written a spinlock for anything in production, but Disruptor has shown me that there are cases for it.
    • bob10291 hour ago
      To be really pedantic, it&#x27;s a spin wait, not a spin lock in disruptor. You are waiting for a sequence, not mutually excluding some resource. Many threads can watch the same volatile at the same time without blocking each other.
    • BoingBoomTschak1 hour ago
      I think Linus says it well: <a href="https:&#x2F;&#x2F;www.realworldtech.com&#x2F;forum&#x2F;?threadid=189711&amp;curpostid=189723" rel="nofollow">https:&#x2F;&#x2F;www.realworldtech.com&#x2F;forum&#x2F;?threadid=189711&amp;curpost...</a>
    • sedatk1 hour ago
      It’s one of the secret ingredients to avoid a Big Kernel Lock™.
    • ignoramous1 hour ago
      &gt; <i>had not heard of anyone actually using a spinlock in production code</i><p>Go stdlib <i>sync.Mutex</i> uses spins: <a href="https:&#x2F;&#x2F;victoriametrics.com&#x2F;blog&#x2F;go-sync-mutex" rel="nofollow">https:&#x2F;&#x2F;victoriametrics.com&#x2F;blog&#x2F;go-sync-mutex</a> &#x2F; <a href="https:&#x2F;&#x2F;archive.vn&#x2F;BIb7F" rel="nofollow">https:&#x2F;&#x2F;archive.vn&#x2F;BIb7F</a>
      • jmgao48 minutes ago
        Optimistically spinning for a bit before falling back to futex or equivalent is <i>very</i> different from a spinlock.
    • mathisfun1231 hour ago
      not all architectures have atomic cas
      • loeg11 minutes ago
        Real architectures you&#x27;d run more than a single thread on? Such as?
  • dalvrosa2 days ago
    Thanks for sharing! Happy to get feedback :)<p>Note that I don&#x27;t recommend spinlock for most cases, only when there is a 1:1 mapping between threads and phsycal CPU cores, and only after measuring
    • loeg9 minutes ago
      Spinlocks are unsuitable for situations where you can be involuntarily context switched (the vast majority of userspace programs). Probably worth mentioning that.
  • jeffbee1 hour ago
    This would have different answers depending on if it ran on a machine with a more closely-shared cache, right? For example on an Intel efficiency core cluster where 4 cores share an L2.