title: Mutex dynamic allocated
body: <p>I sucessfully used a static mutex, but i got problem with dynamic version.
  In output a variable should be equal to zero..
  Please help.
  Is any better way to write this?</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #include&lt;stdlib.h&gt;
  #include&lt;pthread.h&gt;
  #define N 20
  
  int beers=20;
  
  void* drink(void*);
  
  //pthread_mutex_t lockk = PTHREAD_MUTEX_INITIALIZER;
  typedef struct st1{
    pthread_mutex_t mutex;
    int val;
  }my_struct_t;
  
  int main(){
    pthread_t th[N];
    int i;
    void* return_val;
  
    my_struct_t *data;
      data = malloc(sizeof(my_struct_t));
      data-&gt;val = 20;
      pthread_mutex_init(&amp;data-&gt;mutex,NULL);
  
    for(i=0;i&lt;N;i++)
      pthread_create(&amp;th[i], NULL, drink, &amp;data);
    for(i=0;i&lt;N;i++)
      pthread_join(th[i], &amp;return_val);
    pthread_mutex_destroy(&amp;data-&gt;mutex);
    printf("%d\n", beers);
  }
  
  void* drink(void* p){
    int i;
      my_struct_t *data = (my_struct_t *)p;
      pthread_mutex_lock(&amp;data-&gt;mutex);
      beers--;
      pthread_mutex_unlock(&amp;data-&gt;mutex);
  
    //}
    return NULL;
  }
  </code></pre>
  
  <p>Where i got fail? :)</p>