title: Trouble compiling .c and header files with gcc in Ubuntu WSL
body: <p>I am trying to compile some c code with multithreading and for some reason I'm getting a segmentation fault in the Ubuntu WSL terminal when I try to run:</p>
  <blockquote>
  <p>gcc -o mashu concurrent_list.c concurrent_list.h</p>
  </blockquote>
  <p>The files I am trying to run are the following:</p>
  <p>concurrent_list.c:</p>
  <pre><code>#include &lt;pthread.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;limits.h&gt;
  #include &quot;concurrent_list.h&quot;
  
  struct node {
    int value;
    node* next;
    pthread_mutex_t* lock;
    // add more fields
  };
  
  struct list {
    // add fields
    node* head;
    pthread_mutex_t* lock;
  };
  
  void print_node(node* node)
  {
    // DO NOT DELETE
    if(node)
    {
      printf(&quot;%d &quot;, node-&gt;value);
    }
  }
  
  list* create_list()
  {
    // add code here
    list* l = malloc(sizeof(list));
    if(l == NULL){
      printf(&quot;malloc error&quot;);
    }
    l-&gt;head = NULL;
    l-&gt;head-&gt;next = NULL;
    if(pthread_mutex_init(l-&gt;lock, NULL) != 0){
       printf(&quot;mutex init failed\n&quot;);
    }
    if(pthread_mutex_init(l-&gt;head-&gt;lock, NULL) != 0){
      printf(&quot;mutex init failed\n&quot;);
    }
    return l;
  }
  
  void delete_list(list* list)
  {
    // add code here
    pthread_mutex_lock(list-&gt;lock);
    node* head = list-&gt;head;
    node* next = head-&gt;next;
    while(next-&gt;next != NULL){
      free(head);
      next = next-&gt;next;
      head = next;
    }
    pthread_mutex_unlock(list-&gt;lock);
    free(list);
  }
  
  void insert_value(list* list, int value)
  {
    // add code here
  
    // if the list is empty
    pthread_mutex_lock(list-&gt;lock);
    if(list-&gt;head == NULL){
      list-&gt;head-&gt;value = value;
      pthread_mutex_unlock(list-&gt;lock);
    }
    else{
      // init newnode
      node* newNode = malloc(sizeof(node));
      if(!newNode){
        printf(&quot;malloc failed\n&quot;);
      }
      newNode-&gt;value = value;
      newNode-&gt;next = NULL;
      if(pthread_mutex_init(newNode-&gt;lock, NULL) != 0){
        printf(&quot;mutex init failed\n&quot;);
      }
  
      node* curr = list-&gt;head;
      // lock the list and the first node 
      pthread_mutex_lock(curr-&gt;lock);
      if(curr-&gt;next == NULL){ // first and only node at the start of a list
        if(curr-&gt;value &gt; value){  // insert the newnode at the beggining
          list-&gt;head = newNode;
          newNode-&gt;next = curr;
        }else{
          curr-&gt;next = newNode;
        }
        pthread_mutex_unlock(list-&gt;lock);
        pthread_mutex_unlock(curr-&gt;lock);
        // finished the insert
      }
      else{
        node* prev = curr;
        curr = curr-&gt;next;
        pthread_mutex_unlock(list-&gt;lock);
        pthread_mutex_lock(curr-&gt;lock);
        while(curr-&gt;value &lt; value &amp;&amp; curr-&gt;next != NULL){
          pthread_mutex_unlock(prev-&gt;lock);
          prev = curr;
          curr = curr-&gt;next;
          pthread_mutex_lock(curr-&gt;lock);
        }
        if(curr-&gt;next == NULL){
          curr-&gt;next = newNode;
        }else{
          prev-&gt;next = newNode;
          newNode-&gt;next = curr;
        }
        pthread_mutex_unlock(prev-&gt;lock);
        pthread_mutex_unlock(curr-&gt;lock);
      }
    }
  }
  
  void remove_value(list* list, int value)
  {
    // add code here
    
    
    
  }
  
  void print_list(list* list)
  {
    // add code here
    node* curr = list-&gt;head;
    pthread_mutex_lock(list-&gt;lock);
    if(curr != NULL){
      pthread_mutex_unlock(list-&gt;lock);
      while(curr != NULL){
        pthread_mutex_lock(curr-&gt;lock);
        print_node(curr);
        curr = curr-&gt;next;
        pthread_mutex_unlock(curr-&gt;lock);
      }
    }
    printf(&quot;\n&quot;); // DO NOT DELETE
  }
  
  void count_list(list* list, int (*predicate)(int))
  {
    int count = 0; // DO NOT DELETE
  
    // add code here
  
    printf(&quot;%d items were counted\n&quot;, count); // DO NOT DELETE
  }
  
  int main(){
    list* l = create_list();
    printf(&quot;1\n&quot;);
    insert_value(l,6);
    printf(&quot;2\n&quot;);
    insert_value(l,12);
    insert_value(l,3);
    insert_value(l,19);
    insert_value(l,8);
    printf(&quot;3\n&quot;);
    print_list(l);
    printf(&quot;4\n&quot;);
    delete_list(l);
  }
  </code></pre>
  <p>concurrent_list.h:</p>
  <pre><code>typedef struct node node;
  typedef struct list list;
  
  list* create_list();
  void delete_list(list* list);
  void print_list(list* list);
  void insert_value(list* list, int value);
  void remove_value(list* list, int value);
  void count_list(list* list, int (*predicate)(int));
  </code></pre>
  <p>The thrown error when compiling is:</p>
  <blockquote>
  <p>Segmentation fault</p>
  </blockquote>
  <p>Am I accessing illegal memory, not compiling correctly or am I using mutex threads wrong?</p>
  <p>Any help would be appriciated.</p>