title: C - Creating n threads
body: <p>I am working on the following function. This function should create n threads. Also it should print the tid of the child thread. But at the moment I am little bit confused. When I execute it and for example I create 5 threads, it returns all the time the same tid. As far as I understood the tid is the thread Id of the caller. Does the same caller call all these threads or did I something wrong.
  Here's the code:</p>
  
  <pre><code>void spawnThreads( unsigned int n) {
     int threads = n, ret = -1;
     pthread_t * thread = malloc(sizeof(pthread_t)*threads);
     pid_t tid;
     int i;
     for(i = 0; i &lt; threads; i++) {
         ret = pthread_creation(&amp;thread[i], NULL, (void *(*)(void *)) foo, NULL); // foo does not do anything
  
         if( ret != 0) {
             printf("pthread error!\n");
         }
  
         tid = syscall(SYS_gettid);
         printf("%d %d\n", i, tid);
         printf("I just created thread %d\n", i);
  
         pthread_join(thread[i],NULL);
  }
  
  void * foo(void) {
      return NULL;
  }
  </code></pre>
  
  <p>For example, I get the for the following input <strong>spawnThreads(4)</strong>
  the following output:</p>
  
  <pre><code> 0 2411
   I just created thread 0
  
   1 2411
   I just created thread 1
  
   2 2411
   I just created thread 2
  
   3 2411
   I just created thread 3
  </code></pre>
  
  <p>To sum up, the function should print <strong>>i&lt; >tid&lt;</strong>. <strong>>tid&lt;</strong> denotes the TID of the child and <strong>>i&lt;</strong> runs from 1 to n.</p>
  
  <p>But why do I get four times the same tid? What did I do wrong? I would appreciate it, if someone could explain me what went wrong.</p>