Thread Sequence Number In Linux - Unix

This is a discussion on Thread Sequence Number In Linux - Unix ; Sorry if this is slightly off-topic, I don't know where else to post this. I am porting an application that currently runs on HP-UX and AIX to Linux. HP-UX and AIX both have pthread functions to get a unique sequence ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: Thread Sequence Number In Linux

  1. Thread Sequence Number In Linux

    Sorry if this is slightly off-topic, I don't know where else to post
    this.

    I am porting an application that currently runs on HP-UX and AIX to
    Linux.

    HP-UX and AIX both have pthread functions to get a unique sequence
    number identifying a particular thread.

    For HP, it is pthread_getsequence_np(...)

    For AIX, it is pthread_getunique_np(...)

    I have noticed that neither of these methods exist on my Linux
    distribution.

    Is there a Linux equivalent to the aforementioned methods?

    I did a quick flyby in pthread.h, but didn't notice anything that
    could help me.

    Many thanks,
    AJ

  2. Re: Thread Sequence Number In Linux

    On Dec 6, 3:10 pm, aj wrote:
    > Sorry if this is slightly off-topic, I don't know where else to post
    > this.


    comp.programming.threads

  3. Re: Thread Sequence Number In Linux

    aj writes:

    > For HP, it is pthread_getsequence_np(...)
    > For AIX, it is pthread_getunique_np(...)
    >
    > Is there a Linux equivalent to the aforementioned methods?


    I don't believe there is such a function on Linux, nor is one
    usually necessary, since the same result is trivial to achieve in a
    completely portable way: just count how many threads you've created
    so far, and pass the sequence number to your 'thread routine',
    which can then store it in a thread-specific variable.

    If you are writing a library, and do not have control over thread
    creation, you can still get the answer pretty easily:

    // warning: untested code ...

    size_t get_sequence()
    {
    static size_t seq;
    __thread size_t my_seq;
    static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;

    if (!my_seq) {
    // haven't see this thread yet
    pthread_mutex_lock(&mtx);
    my_seq = ++seq;
    pthread_mutex_unlock(&mtx);
    }
    return my_seq;
    }


    Cheers,
    --
    In order to understand recursion you must first understand recursion.
    Remove /-nsp/ for email.

  4. Re: Thread Sequence Number In Linux

    On Dec 6, 11:57 pm, Paul Pluzhnikov
    wrote:
    > aj writes:
    > > For HP, it is pthread_getsequence_np(...)
    > > For AIX, it is pthread_getunique_np(...)

    >
    > > Is there a Linux equivalent to the aforementioned methods?

    >
    > I don't believe there is such a function on Linux, nor is one
    > usually necessary, since the same result is trivial to achieve in a
    > completely portable way: just count how many threads you've created
    > so far, and pass the sequence number to your 'thread routine',
    > which can then store it in a thread-specific variable.
    >
    > If you are writing a library, and do not have control over thread
    > creation, you can still get the answer pretty easily:
    >
    > // warning: untested code ...
    >
    > size_t get_sequence()
    > {
    > static size_t seq;
    > __thread size_t my_seq;
    > static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
    >
    > if (!my_seq) {
    > // haven't see this thread yet
    > pthread_mutex_lock(&mtx);
    > my_seq = ++seq;
    > pthread_mutex_unlock(&mtx);
    > }
    > return my_seq;
    > }
    >
    > Cheers,
    > --
    > In order to understand recursion you must first understand recursion.
    > Remove /-nsp/ for email.


    Thanks for the insight, Paul. I greatly appreciate it.

+ Reply to Thread