Why are the thread ids not unique? : <p>I am trying to create a thread for each file (targeting Linux). The number of files is based off of the number of files in the current directory.  Thus, I am trying to create a dynamic number of threads.</p>

<p>After reading many SO questions and answers about dynamic thread creation and additional research, I came up with the following code.  It is my understanding that to check if a thread was created for each file I can call gettid() which returns the caller's thread ID, and in a multithreaded process, all threads have the same PID, but each one has a unique TID. </p>

<p>However, the TID I am printing is not unique, and I am not understanding why.</p>

<pre><code>char **filenames;
int file_cnt;
DIR *dir;


int main(int argc, char *argv[]) {

    int i;
    long tid;

    //atexit(cleanup);
    get_filenames();  //gets all files in the current directory

    printf("There are %d files:\n", file_cnt);
    pthread_t file[file_cnt];
    for(i = 0; i &lt; file_cnt; i++) {
        printf("%s\n", filenames[i]);
        tid = syscall(SYS_gettid);
        pthread_create(&amp;(file[i], NULL, get_filenames, (void *)file[i]);
        printf("%ld\n", tid);
    }
    return EXIT_SUCCESS;
}
</code></pre>

<p>Any suggestions as to why the threads are not unique? I am new to multithreading and am not understanding where I went wrong despite a lot of research.</p>
