title: threads safe linked list fine grained in C
body: <p>I'm implementing a linked list with fine grained locking, meaning a lock in every node.</p>
  
  <p>When I'm adding a new node, I want to pass only 2 arguments: key and value.
  How can I make sure each node has a different lock? The lock is implemented by <code>pthread_mutex_t</code>.</p>
  
  <p>This is my implementation to add:</p>
  
  <pre><code>int setos_add(int key, void* value)
  {
  volatile setos_node* node = head;
  volatile setos_node* prev;
  
  pthread_mutex_t new_mutex;  //wrong: it will put the same lock to everyone
  
  //lock the head node
  if(pthread_mutex_lock(&amp;(node-&gt;mutex)) == 0){
      printf("failed locking\n");
      exit(1);
  }
  
  // go through nodes until the key of "node" exceeds "key"
  // new node should then be between "prev" and "node"
  while ((node != NULL) &amp;&amp; (node-&gt;key &lt; key)) {
      prev = node;  //already locked
      node = node-&gt;next;
      //locking 2 nodes each time
      if (node != NULL){
          if(pthread_mutex_lock(&amp;(node-&gt;mutex)) == 0){
              printf("failed locking\n");
              exit(1);
          }
      }
      if (node -&gt; key &lt; key){  //else: we need also preve staying locked for       the insertions
          if (pthread_mutex_unlock(&amp;(prev-&gt;mutex)) != 0){
              printf("failed unlocking\n");
              exit(1);
          }
      }
  }
  
  // if node is same as our key - key already in list
  if ((node != NULL) &amp;&amp; (node-&gt;key == key)){
  
      if (node != NULL){  //not the end of the list
          if (pthread_mutex_unlock(&amp;(node-&gt;mutex)) != 0){
              printf("failed unlocking\n");
              exit(1);
          }
      }
  
      if (prev != NULL){ //not the begining of the list
          if (pthread_mutex_unlock(&amp;(prev-&gt;mutex)) != 0){
              printf("failed unlocking\n");
              exit(1);
          }
      }
      return 0;
  }
  
  // allocate new node and initialize it
  setos_node* new_node = (setos_node*) malloc(sizeof(setos_node));
  new_node-&gt;key = key;
  new_node-&gt;value = value;
  new_node-&gt;mutex = new_mutex;  //to change
  if(pthread_mutex_init(&amp;(new_node-&gt; mutex), NULL) != 0){
      printf("failed init mutex");
      exit(1);
  }
  
  
  // place node into list
  new_node-&gt;next = node;
  prev-&gt;next = new_node;
  
  if (node != NULL){ //not the end of the list
      if (pthread_mutex_unlock(&amp;(node-&gt;mutex)) != 0){
          printf("failed unlocking\n");
          exit(1);
      }
  }
  
  if (prev != NULL){ //not the begining of the list
      if (pthread_mutex_unlock(&amp;(prev-&gt;mutex)) != 0){
          printf("failed unlocking\n");
          exit(1);
  
      }
  }
  
  return 1;
  }
  </code></pre>
  
  <p>You can see that in the third line I'm defining the <code>pthread_mutex_t</code> to the node. But doing so, will each new node have the same <code>pthread_mutex_t</code>?</p>
  
  <p>How can I do it right?</p>