title: Linux kernel mutexes
body: <p>I am reading "Linux device drivers 3rd edition", the chapter about concurrrency and race conditions. There is an example I don't completely understand; they are talking about the a common pattern in kernel programming, when one needs to initiate activity (fo rexample, new kernel thread or user process, request for existing process, or hardware based action) outside of the current thread, wait for that activity to complete. The example of not very effective solution is:</p>
  
  <pre><code>struct semaphore sem;
  init_MUTEX_LOCKED(&amp;sem);
  start_external_task(&amp;sem);
  down(&amp;sem);
  </code></pre>
  
  <p>Then they suggest the external task to call up(&amp;sem) when its work is done.</p>
  
  <p>I don't understand why can't we do it this way:</p>
  
  <pre><code>struct semaphore sem;
  down(&amp;sem);
  start_external_task(&amp;sem);
  </code></pre>
  
  <p>Why is is necessary to create mutex in locked state and then acquire the mutex after the task has been started?</p>
  
  <p>Looking forward to hearing from you! Thanks.</p>