The Python interpreter provides low-level support for thread-local storage (TLS) which wraps the underlying native TLS implementation to support the Python-level thread-local storage API (
). The CPython C level APIs are similar to those offered by pthreads and Windows: use a thread key and functions to associate a void* value per thread.
A
does not need to be
when calling these functions; they supply their own locking.
Note that Python.h does not include the declaration of the TLS APIs, you need to include pythread.h to use thread-local storage.
Note
None of these API functions handle memory management on behalf of the void* values. You need to allocate and deallocate them yourself. If the void* values happen to be
*, these functions don’t do refcount operations on them either.
Thread-specific storage API
The thread-specific storage (TSS) API was introduced to supersede the use of the existing TLS API within the CPython interpreter. This API uses a new type
instead of int to represent thread keys.
Added in version 3.7.
See also
“A New C-API for Thread-Local Storage in CPython” (
)
typePy_tss_t
This data structure represents the state of a thread key, the definition of which may depend on the underlying TLS implementation, and it has an internal field representing the key’s initialization state. There are no public members in this structure.
When
is not defined, static allocation of this type by
is allowed.
Py_tss_NEEDS_INIT
This macro expands to the initializer for
variables. Note that this macro won’t be defined with
.
Dynamic allocation
Dynamic allocation of the
, required in extension modules built with
, where static allocation of this type is not possible due to its implementation being opaque at build time.
*PyThread_tss_alloc()
Part of the
since version 3.7.Return a value which is the same state as a value initialized with
, or NULL in the case of dynamic allocation failure.
voidPyThread_tss_free(
*key)
Part of the
since version 3.7.Free the given key allocated by
, after first calling
to ensure any associated thread locals have been unassigned. This is a no-op if the key argument is NULL.
Note
A freed key becomes a dangling pointer. You should reset the key to NULL.
Methods
The parameter key of these functions must not be NULL. Moreover, the behaviors of
and
are undefined if the given
has not been initialized by
.
intPyThread_tss_is_created(
*key)
Part of the
since version 3.7.Return a non-zero value if the given
has been initialized by
.
intPyThread_tss_create(
*key)
Part of the
since version 3.7.Return a zero value on successful initialization of a TSS key. The behavior is undefined if the value pointed to by the key argument is not initialized by
. This function can be called repeatedly on the same key – calling it on an already initialized key is a no-op and immediately returns success.
voidPyThread_tss_delete(
*key)
Part of the
since version 3.7.Destroy a TSS key to forget the values associated with the key across all threads, and change the key’s initialization state to uninitialized. A destroyed key is able to be initialized again by
. This function can be called repeatedly on the same key – calling it on an already destroyed key is a no-op.
intPyThread_tss_set(
*key, void*value)
Part of the
since version 3.7.Return a zero value to indicate successfully associating a void* value with a TSS key in the current thread. Each thread has a distinct mapping of the key to a void* value.
void*PyThread_tss_get(
*key)
Part of the
since version 3.7.Return the void* value associated with a TSS key in the current thread. This returns NULL if no value is associated with the key in the current thread.
Legacy APIs
Deprecated since version 3.7: This API is superseded by the
thread-specific storage (TSS) API
.
Note
This version of the API does not support platforms where the native TLS key is defined in a way that cannot be safely cast to int. On such platforms,
will return immediately with a failure status, and the other TLS functions will all be no-ops on such platforms.
Due to the compatibility problem noted above, this version of the API should not be used in new code.
intPyThread_create_key()
Part of the
.voidPyThread_delete_key(intkey)
Part of the
.intPyThread_set_key_value(intkey, void*value)
Part of the
.void*PyThread_get_key_value(intkey)
Part of the
.voidPyThread_delete_key_value(intkey)
Part of the
.voidPyThread_ReInitTLS()
Part of the
.