title: pthread (segmentation fault)
body: <p>I'm Korean and I'm not good at English but if you give me a comment down there <br>
  I will be very pleased and will try to understand it.</p>
  
  <p>I created, for example, 10 threads and tried to join them after creation and return the value.<br>
  But when I join the last thread, I get a segmentation fault. </p>
  
  <p>The result comes out like this..</p>
  
  <pre><code>Before Thread 1 create
  After Thread 1 create
  Before Thread 0 create
  After Thread 0 create
  Before Thread 1 join
  After Thread 1 join
  Before Thread 0 join
  Segmentation Fault(core dumped)
  </code></pre>
  
  <p>when I create 4 threads it's like</p>
  
  <pre><code>Before Thread 3 create
  After Thread 3 create
  Before Thread 2 create
  After Thread 2 create
  Before Thread 1 create
  After Thread 1 create
  Before Thread 0 create
  After Thread 0 create
  Before Thread 3 join
  After Thread 3 join
  Before Thread 2 join
  After Thread 2 join
  Before Thread 1 join
  After Thread 1 join
  Before Thread 0 join
  Segmentation Fault(core dumped)
  </code></pre>
  
  <p>I can't seem to find why.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;pthread.h&gt;
  #include &lt;unistd.h&gt;
  
  pthread_mutex_t mutex_lock;
  
  struct arg_struct {
          int a;
          int b;
  };
  
  void *t_function(void *data) {
          pthread_mutex_lock(&amp;mutex_lock);
  
          struct arg_struct *arg = (struct arg_struct *)data;
          long int s;
  
          s = arg-&gt;a;
  
          pthread_mutex_unlock(&amp;mutex_lock);
  
          return (void **)s;
  }
  
  int main()
  {
          int i;
  
          pthread_t p_thread[2];
          int thr_id;
          int status;
  
          struct arg_struct arg[2];
  
          for(i = 1; i &gt;= 0; i--) {
                  arg[i].a = i;
                  arg[i].b = i;
          }
  
          pthread_mutex_init(&amp;mutex_lock, NULL);
  
          for(i = 1; i &gt;= 0; i--) {
                  printf("Before Thread %d create\n", i);
                  thr_id = pthread_create(&amp;p_thread[i],NULL, t_function, (void *)&amp;arg[i]);
                  printf("After Thread %d create\n", i);
                  usleep(1000);
          }
  
          int temp[2];
  
          for(i = 1; i &gt;= 0; i--) {
                  printf("Before Thread %d join\n", i);
                  pthread_join(p_thread[i], (void**)&amp;status);
                  printf("After Thread %d join\n", i);
                  temp[i] = status;
          }i
  
          printf("%d%d", temp[1], temp[0]);
  
          pthread_mutex_destroy(&amp;mutex_lock);
  
          return 0;
  }
  </code></pre>