title: When the main thread exits, do other threads also exit?
body: <p>I have a problem about main threads and other threads in the same process. When the main function returns, do the other threads exit too? I am confused about this.</p>
  
  <p>Consider the following test code:</p>
  
  <pre><code>void* test1(void *arg)
  {
      unsigned int i = 0;
      while (1){
          i+=1;
      }
      return NULL;
  }
  
  void* test2(void *arg)
  {
      long double i = 1.0;
      while (1){
          i *= 1.1;
      }
      return NULL;
  }
  
  void startThread ( void * (*run)(void*), void *arg) {
    pthread_t t;
    pthread_attr_t attr;
    if (pthread_attr_init(&amp;attr) != 0
        || pthread_create(&amp;t, &amp;attr, run, arg) != 0
        || pthread_attr_destroy(&amp;attr) != 0
        || pthread_detach(t) != 0) {
      printf("Unable to launch a thread\n");
      exit(1);
    }
  }
  
  int main()
  {
      startThread(test1, NULL);
      startThread(test2, NULL);
  
      sleep(4);
      printf("main thread return.\n");
  
      return 0;
  }
  </code></pre>
  
  <p>When the "main thread return." prints out, thread test1 and test2 also exit， can anyone tell me why?</p>