Reputation: 30765
My question is if there is a function which returns thread ID other than pthread_self and gettid. The problem with pthread_self is that it returns an address while gettid returns system wide global tid. I want relative thread id, so thread ids should be 0, 1, 2, 3, etc and not addresses as in case of pthread_self.
Upvotes: 0
Views: 1507
Reputation: 182649
There isn't. If you really need it (which I doubt), implement your own mechanism.
static int thread_count;
static __thread int my_thread_id;
Here's an example:
static int thread_count;
static __thread int my_thread_id;
static pthread_mutex_t start_mtx = PTHREAD_MUTEX_INITIALIZER;
static void *th_function(void *arg)
{
pthread_mutex_lock(&start_mtx);
my_thread_id = thread_count++;
pthread_mutex_unlock(&start_mtx);
/* rest of function */
}
Obviously you could also use thread-specific data (pthread_setspecific
etc) (which is standard).
Upvotes: 4