Search code examples
cmultithreadingpthreads

Inherit pthread local data


I have trying to see if there is in pthread a way to inherit thread local data,like InheritableThreadLocal in Java.

What I mean by that is that if Thread A, with a key my_key = 2 spawns a Thread B, then Thread B is spawned with the same key my_key = 2.

void callback(...) {...}

main()


library_set_callback_on_data_retrieved(callback)

--- Start thread A ---> call_black_box_library(library_specific_params...) ---> calls callback in another_thread

--- Start thread B ---> call_black_box_library(library_specific_params...) --> calls callback in another_thread

In my callback, I would need a specific identifier to know from which thread the callback originates, but they are ran in another thread by the library and I do not know how to do that.

Thanks in advance


Solution

  • I have trying to see if there is in pthread a way to inherit thread local data,like InheritableThreadLocal in Java.

    TL;DR: No, pthreads does not offer anything analogous to InheritableThreadLocal. Nor does it in general attribute significance to which thread starts which, unlike Java.

    The closest pthreads feature is thread-specific data, the primary interfaces to which are pthread_key_create(), pthread_setspecific() and pthread_getspecific(), and pthread_key_delete(). The documentation for pthread_key_create() explains that

    Upon key creation, the value NULL shall be associated with the new key in all active threads. Upon thread creation, the value NULL shall be associated with all defined keys in the new thread.

    No exceptions are documented, so no, this feature does not provide for one thread to inherit thread-specific data from another.

    Going a bit further afield, since C11, the C language has had built-in support for threads, including a set of functions closely analogous to pthreads. Its analogs to the pthreads functions discussed above are tss_create(), tss_set(), tss_get(), and tss_delete(), and they have the same semantics with respect to (non-)inheritance of thread-specific storage.

    C11 also defines a new "thread" storage duration, specified via the new _Thread_local storage class specifier. For most purposes, this is a simpler entry into thread-local data. But this feature does not provide for inheriting data from one thread to another either. Each thread-local object is initialized when its thread is created, either to the value specified by its initializer, or if it has no initializer then to a default value that, approximately speaking, is all-zero.

    In my callback, I would need a specific identifier to know from which thread the callback originates, but they are ran in another thread by the library and I do not know how to do that.

    Your callback could use any of those forms of thread-local data to recognize that multiple calls to that or other callbacks are performed by the same thread, and to share data among those calls, but only on a single-thread basis. These mechanisms do not provide for relating the callback calls of one thread to those of any other.