c - thread-local storage overhead -
Assume that there are some non-reentrant functions that use global variables:
Int i; Void foo (zero) {/ * Modified i * /} And then, I want to use this function in the multithreaded code, so I can change the code like this: / P>
void foo (int i) {/ * modified i * /} or, using the gcc __thread specifier, more simple:
__thread int i; After all, the advantage is that I do not need to change any other code which calls Fu (). P> My question is, how many overheads are there for thread-local storage? Is there no clear issue with TLS? Is there some overhead if I modify the TLSad variable by different pointer, like:
__thread int i; Zero foo (zero) {int * p = & amp; I; / * P pointer * /} thanks
And again, I want to use this function in multi-thread code, so I can change the code like this:
void foo (int i) {/ This will surely not work because you will only modify a copy of i if you If you want to make changes, you should instead change the int * or int & amp; must pass. The use of TLS will definitely not be important on any custom approach to overhead (either in space or time), you can follow the same functionality to implement the compiler in practice TLS is a global A slot, in the data structure, assigns a slot by dynamically allocating "slots" which keeps your thread-local variable.
When you use thread-local variables on the runtime, there is an additional level of Indirection: first to run Runtime to reach the appropriate thread-local variable table for the current thread, and after that Get the value from the table.
If you intend to do this:
__ thread int i; Zero foo (zero) {int * p = & amp; I; I am modifying using / p pinter * /} , then there is no need to access i using an indicator. Think of i as a global variable, which is a different value for each running thread. You will not need to stick to an ordinary global through an indicator, so there is no need to use pointer with a thread-local variable. Finally, thread-local storage is not really meant that a large number of variables per thread (there are compiler-dependent limitations on the TLS table size), but there is something that you can easily do Can: Insert several variables into a struct and the structure indicator for thread-locals
Comments
Post a Comment