[Python-checkins] [Python-Dev] cpython: Close #12028: Make threading._get_ident() public, rename it to

Charles-François Natali neologix at free.fr
Tue May 31 09:17:25 CEST 2011


>> +.. function:: get_ident()
>> +
>> +   Return the 'thread identifier' of the current thread.  This is a nonzero
>> +   integer.  Its value has no direct meaning; it is intended as a magic cookie
>> +   to be used e.g. to index a dictionary of thread-specific data.  Thread
>> +   identifiers may be recycled when a thread exits and another thread is
>> +   created.
>
> That's not quite true - the Thread id isn't relinquished until the
> Thread object itself is destroyed, rather than when the underlying
> thread finishes execution (i.e. the lifecycle of a_thread.ident is the
> same as that of id(a_thread)).
>

I'm not sure I understand, Nick.
Since threads are started detached, their thread ID (e.g. returned by
pthread_self() on pthreads) can be reused as soon as the underlying OS
thread exits (i.e. returns from Modules/_threadmodule.c:t_boostrap) :

On a Linux kernel with NPTL:

$ cat /tmp/test.py
import threading

def print_ident():
    print(threading._get_ident())

t1  = threading.Thread(target=print_ident)
t2  = threading.Thread(target=print_ident)

t1.start()
t1.join()

t2.start()
t2.join()

print(id(t1), id(t2))
$ ./python /tmp/test.py
-1211954272
-1211954272
(3085561228L, 3083093028L)


I'm just curious, maybe I missed something?

Thanks,

cf


More information about the Python-checkins mailing list