title: C - Pthreads mutex and general headaches
body: <p>Hey guys I was wondering if someone could provide a little help.</p>
  
  <p>I've been trying to teach myself pthreads and with that, mutex locks to get threads running together and using the same structure, whilst not reading and writing to bad data.</p>
  
  <p>My problem at the moment is,</p>
  
  <p>From my thread function, if i call a helper function that might look something similar to the following:</p>
  
  <pre><code>void foo(void *arg)
  {
    Bar *bar = arg;
    pthread_mutex_lock(&amp;mutex);
    bar-&gt;something = 1;
    pthread_mutex_unlock(&amp;mutex);
  }
  </code></pre>
  
  <p>This above helper method doesn't seem to "update" the structure.</p>
  
  <p>But if I run the same code in the thread function, the exact same 4 lines, than this seems to work.</p>
  
  <p>What am I doing wrong? Or how do I fix this? If anyone could provide some reading as well that would be perfect.</p>
  
  <p>EDIT: Sorry guys that was a typo in my code.</p>
  
  <p>Here is the actual code I'm using for the structure.</p>
  
  <pre><code>typedef struct {
      char *buffer[CAR_PARK_SIZE];       
      char *arrival_time[CAR_PARK_SIZE]; 
      int  keep_running;           
      int  size;          
   int  index;     
   } CarStorage;
  
  typedef struct {
   CarStorage parks;
   CarStorage queue;
   int busy;
   } CarPark;
  
  pthread_mutex_t mutex;
  
  void addCar(char *car, void *arg)
  {
   CarPark *_cp = arg;
   pthread_mutex_lock(&amp;mutex);
   printf("Trying to increase size\n");
   _cp-&gt;parks.size = _cp-&gt;parks.size+1;
   pthread_mutex_unlock(&amp;mutex);
  }
  </code></pre>
  
  <p>If the same lines in addCar are in the thread function, it will increase the size, if its in this helper function, it won't.</p>
  
  <p>Here is the calling code</p>
  
  <pre><code>void *carpark_t(void *arg)
  {
      CarPark *_cp = arg; 
      while (_cp-&gt;parks.keep_running)
      {
  
          if (_cp-&gt;queue.size &gt; 0)
          {
  
              addCar(_cp-&gt;queue.buffer[_cp-&gt;queue.index % MAX_QUEUE], &amp;_cp);
              sleep(1);
          }
          else
          {
              printf("[C] no cars in queue\n");
              sleep(5);
          }
      }
  
  }
  </code></pre>