title: Pass argument to multiple threads
body: <p>New to C, I was reading <a href="https://stackoverflow.com/a/19236285/3885972">here</a> about how to properly pass an argument to a thread. What about if this argument needed to be passed to multiple threads? Where/how should I use <code>free()</code>? Say:</p>
  
  <pre><code>void *foo(void *i) {
      int a = *((int *) i);  
      while(1){
          printf("foo running \n");
          sleep(1);
      }
  }
  
  void *bar(void *i) {
      int a = *((int *) i);   
      while(1){
          printf("bar running \n");
          sleep(1);
      }
  }
  
  int main() {
      pthread_t threads[2];
      int i;
      for (i = 0; i &lt; 2; i++ ) {
          int *arg = malloc(sizeof(*arg));
          if ( arg == NULL ) {
              fprintf(stderr, "Couldn't allocate memory for thread arg.\n");
              exit(1);
          }
          *arg = i;
          pthread_create(&amp;threads[0], NULL, foo, arg);
          pthread_create(&amp;threads[1], NULL, bar, arg);
      }
      for (i = 0; i &lt; 2; i++){
          pthread_join(threads[i],NULL);
      }
      return 0;
  }
  </code></pre>
  
  <p>Is calling <code>free(arg);</code> in <code>main</code> after spawning the threads the same thing / safe?</p>