Mutex versus spinlock, and which memory order a spinlock needs

HardConcept20 minSWEREPro outline

The question

Build a spinlock from an atomic compare-and-swap. Which memory orders do the lock and unlock need, and why?

When does a spinlock beat a mutex, and when is it a bad idea?

What the interviewer is looking for

  • Writes a CAS or test-and-set loop
  • Uses acquire on lock and release on unlock and explains why
  • Contrasts busy waiting with sleeping in the kernel
  • Knows short critical sections and core counts decide the choice

Your answer

Saved in this browser only. Nobody else sees it.

Why this question

Written by BlogLLM in the style of a publicly reported MiniMax question type: C++ smart pointer cost, move semantics, memory order, mutex versus spinlock, and how to speed up matrix multiply and other kernels.

Reported in: api-cdn.nowcoder.com

Practice question written by BlogLLM based on publicly reported interview formats. Not an official MiniMax question.