10

I'm doing a library that makes extensive use of a thread local variable. Can you point to some benchmarks that test the performances of the different ways to get thread local variables in C++:

  • C++0x thread_local variables
  • compiler extension (Gcc __thread, ...)
  • boost::threads_specific_ptr
  • pthread
  • Windows
  • ...

Does C++0x thread_local performs much better on the compilers providing it?

Vicente Botet Escriba
  • 4,305
  • 1
  • 25
  • 39

2 Answers2

1

You can always use time.h. Its your friend when testing performance stuff and nothing else is available.

  • What I'm looking for is a benchmark of the use in different contexts. Of course I can do it myself, but I would prefer to reuse one if it exists. Maybe you are interested in Boost.Chrono :) – Vicente Botet Escriba Jun 16 '10 at 23:53
1

These are typically implemented as a simple offset in an array in the thread's private memory space. So, accessing the thread specific variable X, of type T,

T y = X;

roughly translates to,

T y = *(T*)(cur_thread.local_tbl[key_X]);

which is too simple to expect a wide variation in performance between implementations. That said, if you find any such benchmarks, please follow up here.

John
  • 259
  • 1
  • 2
  • 1
    The current Boost implementation is not so simple, as the key is the address of the thread_specific_pointer variable. Compilers can now the keys at compile time, and can make some optimizations. So I expect a wide variation between available implementations, that can be determinant to libraries that make a deep use of thread specific storage. – Vicente Botet Escriba Jun 16 '10 at 23:52