How expensive is Thread::getCurrentThread()?


#1

I have several threads that update very frequently - 60 times a second or more - and some of them need the current thread.

I cache a copy of the current thread to avoid frequent calls to getCurrentThread(), which looks like the sort of thing that might be secretly expensive.  But looking at my code, it strikes me I could substantially simplify it if I didn't cache getCurrentThread()...

 


#2

In the grand scheme of things, it's a pretty cheap function - it's essentially accessing a thread-local variable, so worst case involves traversing a small list and doing a few atomic ops. And if the compiler supports thread-locals, it'll be very quick.


#3

It looks like most compilers don't yet support thread-locals - but good to know.   (Clang is marked as "partial support".)

I'll experiment later with this and report back if there's anything to notice...