How to print out thread id from pthread_t - Unix

This is a discussion on How to print out thread id from pthread_t - Unix ; Hi, I am trying to print out the thread id in my program. static pthread_t GetCurrentThreadID() { return : thread_self(); } void myFunction() { pthread_t id = GetCurrentThreadID(); printf (" thread id %d\n", id); } But I get a negative ...

+ Reply to Thread
Results 1 to 9 of 9

Thread: How to print out thread id from pthread_t

  1. How to print out thread id from pthread_t

    Hi,

    I am trying to print out the thread id in my program.
    static pthread_t GetCurrentThreadID() { return :thread_self(); }

    void myFunction() {
    pthread_t id = GetCurrentThreadID();

    printf (" thread id %d\n", id);

    }

    But I get a negative number: -122112944. Did I do something wrong?
    Why the number is negative?

    Thank you.


  2. Re: How to print out thread id from pthread_t

    On Jun 18, 11:44 pm, Herman.Schu...@gmail.com wrote:
    > Hi,
    >
    > I am trying to print out the thread id in my program.
    > static pthread_t GetCurrentThreadID() { return :thread_self(); }
    >
    > void myFunction() {
    > pthread_t id = GetCurrentThreadID();
    >
    > printf (" thread id %d\n", id);
    >
    > }
    >
    > But I get a negative number: -122112944. Did I do something wrong?
    > Why the number is negative?


    The pthread_t is defined as an unsigned variable, so you should using

    printf("%u")

    to print it out.



  3. Re: How to print out thread id from pthread_t

    On Mon, 18 Jun 2007 15:51:08 -0000 Bin Chen wrote:
    > On Jun 18, 11:44 pm, Herman.Schu...@gmail.com wrote:
    >> Hi,
    >>
    >> I am trying to print out the thread id in my program.
    >> static pthread_t GetCurrentThreadID() { return :thread_self(); }
    >>
    >> void myFunction() {
    >> pthread_t id = GetCurrentThreadID();
    >>
    >> printf (" thread id %d\n", id);
    >>
    >> }
    >>
    >> But I get a negative number: -122112944. Did I do something wrong?
    >> Why the number is negative?


    Because %d is an integer type and prints both positive and negative
    numbers. The thread id happens to be very large and is treated as a
    negative integer.

    > The pthread_t is defined as an unsigned variable,


    No it isn't. It's an opaque type.

    printf("%lu\n", (unsigned long) id);

    -frank

  4. Re: How to print out thread id from pthread_t

    Frank Cusack wrote On 06/18/07 12:28,:
    > On Mon, 18 Jun 2007 15:51:08 -0000 Bin Chen wrote:
    >
    >>On Jun 18, 11:44 pm, Herman.Schu...@gmail.com wrote:
    >>
    >>>Hi,
    >>>
    >>>I am trying to print out the thread id in my program.
    >>> static pthread_t GetCurrentThreadID() { return :thread_self(); }
    >>>
    >>>void myFunction() {
    >>>pthread_t id = GetCurrentThreadID();
    >>>
    >>> printf (" thread id %d\n", id);
    >>>
    >>>}
    >>>
    >>>But I get a negative number: -122112944. Did I do something wrong?
    >>>Why the number is negative?

    >
    >
    > Because %d is an integer type and prints both positive and negative
    > numbers. The thread id happens to be very large and is treated as a
    > negative integer.
    >
    >
    >>The pthread_t is defined as an unsigned variable,

    >
    >
    > No it isn't. It's an opaque type.
    >
    > printf("%lu\n", (unsigned long) id);


    And even that won't always work: pthread_t is *really*
    opaque, and need not be a type to which a cast operator can
    be applied. On at least one implementation, pthread_t is
    a struct.

    --
    Eric.Sosman@sun.com


  5. Re: How to print out thread id from pthread_t

    Eric Sosman writes:
    > Frank Cusack wrote On 06/18/07 12:28,:
    >> On Mon, 18 Jun 2007 15:51:08 -0000 Bin Chen wrote:
    >>>On Jun 18, 11:44 pm, Herman.Schu...@gmail.com wrote:
    >>>>I am trying to print out the thread id in my program.
    >>>> static pthread_t GetCurrentThreadID() { return :thread_self(); }
    >>>>
    >>>>void myFunction() {
    >>>>pthread_t id = GetCurrentThreadID();
    >>>>
    >>>> printf (" thread id %d\n", id);
    >>>>
    >>>>}
    >>>>
    >>>>But I get a negative number: -122112944. Did I do something wrong?
    >>>>Why the number is negative?

    >>
    >>
    >> Because %d is an integer type and prints both positive and negative
    >> numbers. The thread id happens to be very large and is treated as a
    >> negative integer.
    >>
    >>
    >>>The pthread_t is defined as an unsigned variable,

    >>
    >>
    >> No it isn't. It's an opaque type.
    >>
    >> printf("%lu\n", (unsigned long) id);

    >
    > And even that won't always work: pthread_t is *really*
    > opaque, and need not be a type to which a cast operator can
    > be applied. On at least one implementation, pthread_t is
    > a struct.


    So one approach would be to cast the address of the pthread_t object
    to unsigned char*, and use it to print (probably in hexadecimal) the
    values of the bytes making up the representation. This won't
    necessarily be meaningful, but it could be useful, at least to distinguish
    different values.

    For example:

    #include
    #include

    static void print_raw(void *addr, size_t size)
    {
    unsigned char *base = addr;
    size_t i;
    for (i = 0; i < size; i ++) {
    printf("%02x", base[i]);
    }
    }

    int main(void)
    {
    pthread_t self = pthread_self();
    fputs("pthread_self() returns ", stdout);
    print_raw(&self, sizeof self);
    putchar('\n');
    return 0;
    }

    --
    Keith Thompson (The_Other_Keith) kst-u@mib.org
    San Diego Supercomputer Center <*>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"

  6. Re: How to print out thread id from pthread_t

    Eric Sosman wrote:
    > Frank Cusack wrote On 06/18/07 12:28,:
    >> On Mon, 18 Jun 2007 15:51:08 -0000 Bin Chen wrote:
    >>
    >>> On Jun 18, 11:44 pm, Herman.Schu...@gmail.com wrote:
    >>>
    >>>> Hi,
    >>>>
    >>>> I am trying to print out the thread id in my program.
    >>>> static pthread_t GetCurrentThreadID() { return :thread_self(); }
    >>>>
    >>>> void myFunction() {
    >>>> pthread_t id = GetCurrentThreadID();
    >>>>
    >>>> printf (" thread id %d\n", id);
    >>>>
    >>>> }
    >>>>
    >>>> But I get a negative number: -122112944. Did I do something wrong?
    >>>> Why the number is negative?

    >>
    >> Because %d is an integer type and prints both positive and negative
    >> numbers. The thread id happens to be very large and is treated as a
    >> negative integer.
    >>
    >>
    >>> The pthread_t is defined as an unsigned variable,

    >>
    >> No it isn't. It's an opaque type.
    >>
    >> printf("%lu\n", (unsigned long) id);

    >
    > And even that won't always work: pthread_t is *really*
    > opaque, and need not be a type to which a cast operator can
    > be applied. On at least one implementation, pthread_t is
    > a struct.
    >


    Isn't that actually a pointer to a struct?


  7. Re: How to print out thread id from pthread_t

    Golden California Girls wrote:
    > Eric Sosman wrote:
    >> Frank Cusack wrote On 06/18/07 12:28,:
    >>> On Mon, 18 Jun 2007 15:51:08 -0000 Bin Chen
    >>> wrote:
    >>>
    >>> Because %d is an integer type and prints both positive and negative
    >>> numbers. The thread id happens to be very large and is treated as a
    >>> negative integer.
    >>>
    >>>
    >>>> The pthread_t is defined as an unsigned variable,
    >>>
    >>> No it isn't. It's an opaque type.
    >>>
    >>> printf("%lu\n", (unsigned long) id);

    >>
    >> And even that won't always work: pthread_t is *really*
    >> opaque, and need not be a type to which a cast operator can
    >> be applied. On at least one implementation, pthread_t is
    >> a struct.
    >>

    > Isn't that actually a pointer to a struct?
    >

    It might be on some systems, but the point is it is an opaque type, so
    it can be anything.

    --
    Ian Collins.

  8. Re: How to print out thread id from pthread_t

    Golden California Girls wrote:
    > Eric Sosman wrote:
    >> Frank Cusack wrote On 06/18/07 12:28,:
    >>> [...]
    >>> No it isn't. It's an opaque type.
    >>>
    >>> printf("%lu\n", (unsigned long) id);

    >>
    >> And even that won't always work: pthread_t is *really*
    >> opaque, and need not be a type to which a cast operator can
    >> be applied. On at least one implementation, pthread_t is
    >> a struct.

    >
    > Isn't that actually a pointer to a struct?


    There might be such a system, but on the one I'm thinking
    of a pthread_t is a struct of three elements.

    http://devrsrc1.external.hp.com/STKT/impacts/i114.html

    --
    Eric Sosman
    esosman@acm-dot-org.invalid


  9. Re: How to print out thread id from pthread_t

    On Jun 18, 8:44 am, Herman.Schu...@gmail.com wrote:

    > I am trying to print out the thread id in my program.
    > static pthread_t GetCurrentThreadID() { return :thread_self(); }
    >
    > void myFunction() {
    > pthread_t id = GetCurrentThreadID();
    >
    > printf (" thread id %d\n", id);
    >
    > }
    >
    > But I get a negative number: -122112944. Did I do something wrong?
    > Why the number is negative?


    If you want every thread to have an identifier that is a small
    positive number, you need to assign it one. There are any number of
    ways to do this. Using thread-specific data is one way. A lookup table
    mapping pthread_t's to small integers (using 'pthread_equal' to
    traverse it) is another.

    DS


+ Reply to Thread