title: pThread Mutex Locking without Global Mutex
body: <p>All of the tutorials i have seen for Mutex locking with the pThread library have used a Global Mutex Lock:</p>
  
  <p>See:</p>
  
  <p><a href="https://computing.llnl.gov/tutorials/pthreads/#Mutexes" rel="nofollow">https://computing.llnl.gov/tutorials/pthreads/#Mutexes</a></p>
  
  <p><a href="http://www.drdobbs.com/cpp/184401518?pgno=3" rel="nofollow">http://www.drdobbs.com/cpp/184401518?pgno=3</a> (for boost::thread but same context)</p>
  
  <p>What I would like to do is to use Mutex locking where the Global of the main file is out of scope for the function needing to lock the variable. here is an example:</p>
  
  <p>Main.cpp</p>
  
  <pre><code>int main() {
      some initilisation code.
      tree * tree1;
      *Start thread to visualise tree* (see code below)
  
      Mutex Lock:
           delete tree1;
           tree1 = newTree();
      Mutex Unlock
  </code></pre>
  
  <p>visualiser.cpp</p>
  
  <pre><code>visualise(Tree *) {
      Forever:
      Mutex Lock:
           Check for tree change.
           Update tree image.
           Display tree image.
      Mutex Unlock
  </code></pre>
  
  <p>I would like to know if this is possible:</p>
  
  <ol>
  <li>Without global scope mutex locks.</li>
  <li>If possible without passing the mutex to the visualise function.</li>
  </ol>
  
  <p>I understand that this may not be plausable, if not how would i get the global scope variable to the visualiser.cpp, using an extern?</p>
  
  <p>Also if i need to pass the mutex to the function how would i go about doing that?</p>