title: pthread segmentation fault with linked lists - beginner
body: <p>This is the first time I've used pthreads. I'm having trouble because some times my program seg faults and sometimes it does not. I have a few functions in my program that perform some basic tasks (written in C) like creating a linked list, adding an item to a list, and deleting an item from a list. Each function creates it's own copy of a list so I don't think they interact with each other and hence don't need mutexes. Anyway below is my code if anyone has any ideas or if there are any "common" beginner pthread mistakes.</p>
  
  <p>I run each function 1000 times in parallel, some times seg faulting, some times not. I notice it only happens with a combination of these 3 functions.</p>
  
  <p>The process goes like this:
  - create thread
  - run threads in parallel
  - each thread calls dummy function to perform a task a given number of times
  - that dummy function also calls other functions</p>
  
  <p>I think it might have to do with memory usage/allocation because all of these functions have to do with creating/deleting linked list nodes. Thanks a lot.</p>
  
  <p>Here are the creates and joins:</p>
  
  <pre><code> pthread_create(&amp;t7, NULL, (void*)&amp;p4, (void*)var);
   pthread_create(&amp;t8, NULL, (void*)&amp;p5a, (void*)var);
   pthread_create(&amp;t9, NULL, (void*)&amp;p5b, (void*)var);
   pthread_join(t7, NULL);
   pthread_join(t8, NULL);
   pthread_join(t9, NULL);
  </code></pre>
  
  <p>Here are the dummy functions:</p>
  
  <pre><code>void p4(int *nptr){
    int n = *nptr;
    // Get current time
    struct timeval t0, t1;
    gettimeofday(&amp;t0,0);
  
    int i = 0;
    LIST *list = (LIST*)malloc(sizeof(LIST));
        for(i=0;i&lt;n;i++){
      f4(list);
      deleteList(list);
    }
    // Get current time and find time elapsed
    gettimeofday(&amp;t1,0);
    float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
        printf("Successful execution of p4 in %f microseconds.\n", elapsed);
    free(list);
  }
  void p5a(int *nptr){
    int n = *nptr;
    LIST *list = (LIST*)malloc(sizeof(LIST));
    f4(list);
    // Get current time
    struct timeval t0, t1;
    gettimeofday(&amp;t0,0);
  
    int i = 0;
        for(i=0;i&lt;n;i++){
      f5a(list);
    }
    // Get current time and find time elapsed
    gettimeofday(&amp;t1,0);
    float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
    printf("Successful execution of p5a in %f microseconds.\n", elapsed);
  }
  void p5b(int *nptr){
    int n = *nptr;
    LIST *list = (LIST*)malloc(sizeof(LIST));
    f4(list);
    int i = 0;
        for(i=0;i&lt;n;i++){
      f5a(list);
    }
    // Get current time
    struct timeval t0, t1;
    gettimeofday(&amp;t0,0);
    for(i=0;i&lt;n;i++){
      f5b(list);
    }
    // Get current time and find time elapsed
    gettimeofday(&amp;t1,0);
    float elapsed = (t1.tv_sec-t0.tv_sec)*1000000 + t1.tv_usec-t0.tv_usec;
    printf("Successful execution of p5b in %f microseconds.\n", elapsed);
  }
  </code></pre>
  
  <p>Here are the functions used that perform regular tasks:</p>
  
  <pre><code>// FUNCTION: initialize a linked list with pointers and insert a last element
  void f4(LIST *L1){
    // initialize an empty linked list if L1 = null
    if(L1-&gt;head == NULL){
      NODE *n = (NODE *)malloc(sizeof(NODE));
      L1-&gt;head = n;
      L1-&gt;tail = n;
      L1-&gt;tail-&gt;next = NULL;
      n-&gt;data = 1;
    }
    // traverse the linked list to the end
    NODE *iter = L1-&gt;head;
    while(iter-&gt;next != NULL)
      iter = iter-&gt;next;
    // insert a new 2 element
    NODE *new = (NODE *)malloc(sizeof(NODE));
    new-&gt;data = 2; // arbitrary for testing
    new-&gt;next = NULL;
    iter-&gt;next = new;
    L1-&gt;tail = new;
  }
  
  // FUNCTION: add an item to the end of a list (queue)
  void f5a(LIST *list){
    NODE *new = (NODE *)malloc(sizeof(NODE));
    new-&gt;data = 999;
    new-&gt;next = NULL;
    list-&gt;tail-&gt;next = new;
    list-&gt;tail = new;
  }
  
  // FUNCTION: remove an item from the beginning of a list (queue)
  void f5b(LIST *list){
    NODE *remove = list-&gt;head;
    list-&gt;head = list-&gt;head-&gt;next;
    free(remove);
  }
  </code></pre>