title: Multithreading in n-ary tree
body: <p>I have tried to use multithreading in n-ary tree in the lock_it() function. Saw all the tutorials present. But i am unable to come up the code. I only able to remove the deadlocks but i want to maximize the parallel running of  lock_it() function.</p>
  <p>Read at this link to know what lock_it() does.
  <a href="https://www.geeksforgeeks.org/locking-and-unlocking-of-resources-in-the-form-of-n-ary-tree/" rel="nofollow noreferrer">https://www.geeksforgeeks.org/locking-and-unlocking-of-resources-in-the-form-of-n-ary-tree/</a></p>
  <p>How to improve it?</p>
  <pre><code>#include &lt;bits/stdc++.h&gt;
  #include &lt;thread&gt;
  #include &lt;mutex&gt;
  
  using namespace std;
  class Node {
  public:
      char key;
      bool locked;
      int cnt_locked_desc;
      Node* parent;
      vector&lt;Node*&gt; child;
  };
  Node* root = NULL;
  mutex mtx;
  </code></pre>
  <pre><code>class LockUnlock {
  public:
  Node* newNode(char node_key, Node* prt) {
          Node* tmp = new Node;
          (*tmp).key = node_key;
          (*tmp).locked = (*tmp).cnt_locked_desc = 0;
          (*tmp).parent = prt;
          return tmp;
      }
      bool lock_it(Node* node) {  //O(h)
          if ((*node).cnt_locked_desc &gt; 0 || node == NULL)
              return 0;
          for (Node* curr = node; curr != NULL; curr = (*curr).parent)
              if ((*curr).locked)
                  return 0;
          mtx.lock();
          for (Node* curr = node; curr != NULL; curr = (*curr).parent) {
              (*curr).cnt_locked_desc++;
          }
          mtx.unlock();
          (*node).locked = 1;
          return 1;
      }
  };
  </code></pre>