• C++ equivalent of C tss_create

    From jseigh@21:1/5 to All on Mon Oct 7 12:03:42 2024
    tss_create lets you dynamically create thread local storage.
    thread_local is static. Gets resolved at ld time. Doesn't
    work too well if you want a per object instance of thread
    local storage. Something like that in C++?

    Joe Seigh

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From jseigh@21:1/5 to Chris M. Thomasson on Mon Oct 7 18:16:58 2024
    On 10/7/24 15:50, Chris M. Thomasson wrote:
    On 10/7/2024 9:03 AM, jseigh wrote:
    tss_create lets you dynamically create thread local storage.
    thread_local is static.  Gets resolved at ld time.  Doesn't
    work too well if you want a per object instance of thread
    local storage.  Something like that in C++?

    I don't think so. Fwiw, a while back I was trying to port one of my
    thread local memory allocators that used pthread tss to pure C++ and
    gave up. It has important logic in its destructor. The function pointer
    in: pthread_key_create ala tss_create. So, shit happens! ;^o

    I suppose you could use a thread local map with logic on top of that.

    I probably don't need it. I was using it in C to get notification
    when a thread exited for clean up of any resources the thread had
    not explicitly cleaned up. I could come up with a thread exit
    listener if need be.

    Joe Seigh

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Sam@21:1/5 to jseigh on Tue Oct 8 08:26:56 2024
    jseigh writes:

    tss_create lets you dynamically create thread local storage.
    thread_local is static. Gets resolved at ld time. Doesn't
    work too well if you want a per object instance of thread
    local storage. Something like that in C++?

    Not in C++ proper, but gcc supports it:

    https://gcc.gnu.org/onlinedocs/gcc/Thread-Local.html

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From jseigh@21:1/5 to jseigh on Tue Oct 8 17:03:23 2024
    On 10/7/24 12:03, jseigh wrote:
    tss_create lets you dynamically create thread local storage.
    thread_local is static.  Gets resolved at ld time.  Doesn't
    work too well if you want a per object instance of thread
    local storage.  Something like that in C++?

    Joe Seigh


    I figured something out so all good I think.

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From jseigh@21:1/5 to jseigh on Sun Oct 13 13:56:22 2024
    On 10/8/24 17:03, jseigh wrote:
    On 10/7/24 12:03, jseigh wrote:
    tss_create lets you dynamically create thread local storage.
    thread_local is static.  Gets resolved at ld time.  Doesn't
    work too well if you want a per object instance of thread
    local storage.  Something like that in C++?

    Joe Seigh


    I figured something out so all good I think.


    So basically just declare a thread local array. It will have
    fixed size and you will have to manage allocation of array
    slots and dtors and stuff. E.g.

    thread_local void * x2[20];

    Accessing a slot value, if you inline it with a uint64_t
    key on x86 will get you something like

    movq %fs:x2@tpoff(,%rax,8), %rdx

    with %rax containing the index, i.e. the key,

    which will beat calling tss_get which calls
    pthread_getspecific.

    Joe Seigh

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)