Q: uniqueness of pthread_t

From: Ulrich Windl
Date: Mon Mar 24 2014 - 06:41:19 EST


Hi!

I'm programming a little bit with pthreads in Linux. As I understand pthread_t is an opaque type (a pointer address?) that cannot be mapped to the kernel's TID easily. Anyway: Is it expected that when one thread terminates and another thread is created (in fact the same thread again), that the p_thread my be resused?

It seems it just happened when debugging my program (or I made a programming mistake (code compiles clean with -Wall):
---
[...]
cleanup_thread: 1 used connection entries
5888 [^D] (null) -> 192.168.255.18/80
cleanup_thread: leaves with -1: No child processes
8407: [8413](172.20.64.67/54560) handles socket 4
cleanup_thread 139852035340032 terminated
created cleanup_thread 139852035340032
accepting connection (1 handlers)
[...]
---
libgthread-2_0-0-2.22.5-0.8.12.1 (SLES11 SP3 on x86_64)

The code handling the threads looks like this (so the results should be reliable, I guess):

if ( tid != 0 && pthread_tryjoin_np(tid, &ret) == 0 )
{
DEBUG(1) printf("cleanup_thread %ld terminated\\
n",
(long) tid);
tid = 0;
}
[...]
if ( tid != 0 && pthread_tryjoin_np(tid, &ret) == 0 )
{
DEBUG(1) printf("cleanup_thread %ld terminated\\
n",
(long) tid);
tid = 0;
}

(The code above also runs strictly sequential)

And while I'm at it: There's a bug in the man page:
---
PTHREAD_TRYJOIN_NP(3) Linux Programmer's Manual PTHREAD_TRYJOIN_NP(3)



NAME
pthread_tryjoin_np, pthread_timedjoin_np - try to join with a termi-
nated thread

SYNOPSIS
#define _GNU_SOURCE
#include <pthread.h>

int pthread_tryjoin_np(pthread_t thread, void **retval);

int pthread_timedjoin_np(pthread_t thread, void **retval,
const struct timespec *abstime);

Compile and link with -pthread.
---

You must include <features.h> after defining _GNU_SOURCE, and you must do this early in your includes...

I'm not subscribed d to LKML, so please CC your answers to me.

Regards,
Ulrich


--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/