Thread locals in D are really fast. Here are my tests. Maybe compiler could be even more clever and cache thread local before loop to a register and return it to thread local at the end (it's interesting to compare with gdc compiler), but even now matters are very good IMHO.
But: No, threads do not share real local variables.
In 2009, some JVMs implemented ThreadLocal using an unsynchronised HashMap in the Thread. currentThread() object. This made it extremely fast (though not nearly as fast as using a regular field access, of course), as well as ensuring that the ThreadLocal object got tidied up when the Thread died.
With thread local storage (TLS), you can provide unique data for each thread that the process can access using a global index. One thread allocates the index, which can be used by the other threads to retrieve the unique data associated with the index.
How fast is accessing a thread local variables in Linux. From the code generated by the gcc compiler, I can see that is uses the fs
segment register. So apparently, the access to the thread local variable should not cost extra cycles.
However, I keep on reading horror stories about the slowness of thread local variable access. How come? Sure, sometimes different compilers use a different approach than using fs
segment register, but is accessing thread local variable through fs
segment register slow too?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With