title: Is a mutex lock needed around a pointer variable?
body: <p>Is a mutex lock needed around a section of code that involves pointer indirection (where the pointer points to data that is part of a critical section)? An example code:</p>
  
  <pre><code>struct list {
      int i;
      struct list *next;
  };
  
  int modify_second_elem(struct list *head, int val);
  void * func1(void *ptr);
  void * func2(void *ptr);
  
  int modify_second_elem(struct list *head, int val) {
  
      if(head == NULL)
          return 1;
  
      /* Check to see if second element exists.
         Here, I am using indirection to get the next pointer.
         Would I need synchronization here? */
      if(head-&gt;next == NULL)
          return -1;
  
      pthread_mutex_lock(&amp;list_lock);
      (head-&gt;next)-&gt;i = val;
      pthread_mutex_unlock(&amp;list_lock);
  
      return 0;
  
  }
  
  void * func1(void *ptr) {
      struct list *head;
      head = (struct list *) ptr;
      modify_second_elem(head, 4);
  }
  
  void * func2(void *ptr) {
      struct list *head;
      head = (struct list *) ptr;
      modify_second_elem(head, 6);
  }
  
  void main() {
      struct list *el1, *el2, *el3;
      pthread_t th1, th2;
  
      el1 = (struct list *) malloc(sizeof(list));
      el2 = (struct list *) malloc(sizeof(list));
      el3 = (struct list *) malloc(sizeof(list));
  
      el1-&gt;i = 1;
      el1-&gt;next = el2;
      el2-&gt;i = 2;
      el2-&gt;next = el3;
      el3-&gt;i = 3;
      el3-&gt;next = NULL;
  
      pthread_create(&amp;th1, NULL, &amp;func1, (void *) el1);
      pthread_create(&amp;th2, NULL, &amp;func2, (void *) el1);
  
      pthread_join(th1, NULL);
      pthread_join(th2, NULL);
  
      exit(EXIT_SUCCESS);
  }
  </code></pre>