There are several static __thread variables in the static __thread . If the kernel switches the thread context, then there are no problems. If you independently switch the context of stream 1 to stream 2, then stream 2 will see the local variables of stream 1, which does not suit me at all.

Unfortunately, I did not find standard tools to quickly switch between threads, and the coroutine mechanisms do not know anything about the __thread variables.

Hence the question, is it possible and how to switch the context of variables of type __thread ?

PS: If something is not clear, ask, I will clarify. Questions like: why / why - are ignored, who do not like someone - I do not force them to answer.

  • And how do you switch the context of the stream (and what exactly do you put into these words)? And what kind of implementation of pthreads are we talking about? - avp
  • one
    1. swapcontext 2. Standard gcc .... -pthread -lpthread - sergw
  • To my shame, for the first time I learned about these functions from you. Very interesting. Do you want coroutines so to implement or their lightweight threads? - avp
  • @avp The crutch wanted to be screwed to someone else's code, making a coroutine from it and using / modifying the functionality without rewriting someone else's code. - sergw

1 answer 1

It seems that everything is described in detail here: https://www.akkadia.org/drepper/tls.pdf

In short: on the x86-64 architecture, TLS variables are stored in the FS segment. At the same time there is a certain data structure, which allows, among other things, to get the addresses of these variables in the DS segment (so that you can get a pointer to them). The header of this structure is stored at FS: 0.

Further understand yourself :)

PS and in Windows for x64, this structure is located at GS: 0