Re: A question about _thread_ of Linux

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Yes... thats right.
But gettid() and pthread_self() should return the same value according to me.
Can anyone correct me or confirm my theory?
I don't have access to a Linux-box right now, otherwise I would have
tested this theory.


My Linux box is Fedora Core 5, whose pthread library is NPTL.
In my test, the gettid() != pthread_self(). I think the id returned
by pthread_self() is assigned by pthread library.But I am not
sure. My test is shown as follow.

#include <stdio.h>
#include <pthread.h>
#include <sys/types.h>
#include <linux/unistd.h>
#include <errno.h>

_syscall0(pid_t, gettid);

void* func (void* argc)
{
       int i = *((int*)argc);
       printf("In the thread %d:posix tid %u\n", i, pthread_self());
       printf("In the thread %d:pid %u\n", i, getpid());
       printf("In the thread %d:tid %u\n", i, gettid());
}

int main()
{
       pthread_t th[2];
       int i, ret;
       printf("main posix tid: %u\n", pthread_self());
       printf("main pid: %u\n", getpid());
       printf("main tid: %u\n", gettid());
       for (i = 0; i < 2; i++)
               ret = pthread_create (&th[i], NULL, func, (void*)&i);
       for (i = 0; i < 2; i++)
               pthread_join (th[i], NULL);
       return 0;
}

--------------------results----------------------------------------
main posix tid:3086476976
main pid: 14700
main tid: 14700
In the thread 0:posix tid 3086474144
In the thread 0:pid 14700
In the thread 0:tid 14701
In the thread 1:posix tid 3075984288
In the thread 1:pid 14700
In the thread 1:tid 14702

--
Kernelnewbies: Help each other learn about the Linux kernel.
Archive:       http://mail.nl.linux.org/kernelnewbies/
FAQ:           http://kernelnewbies.org/faq/



[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux