title: How efficient is locking and unlocked mutex? What is the cost of a mutex?
body: <p>In a low level language (C, C++ or whatever): I have the choice in between either having a bunch of mutexes (like what pthread gives me or whatever the native system library provides) or a single one for an object.</p>
  
  <p>How efficient is it to lock a mutex? I.e. how many assembler instructions are there likely and how much time do they take (in the case that the mutex is unlocked)?</p>
  
  <p>How much does a mutex cost? Is it a problem to have really <strong>a lot</strong> of mutexes? Or can I just throw as much mutex variables in my code as I have <code>int</code> variables and it doesn't really matter?</p>
  
  <p>(I am not sure how much differences there are between different hardware. If there is, I would also like to know about them. But mostly, I am interested about common hardware.)</p>
  
  <p>The point is, by using many mutex which each cover only a part of the object instead of a single mutex for the whole object, I could safe many blocks. And I am wondering how far I should go about this. I.e. should I try to safe any possible block really as far as possible, no matter how much more complicated and how many more mutexes this means?</p>
  
  <hr>
  
  <p><a href="https://webkit.org/blog/6161/locking-in-webkit/" rel="noreferrer">WebKits blog post (2016) about locking</a> is very related to this question, and explains the differences between a spinlock, adaptive lock, futex, etc.</p>