The Cost of thread_local

后端 未结 2 924
[愿得一人]
[愿得一人] 2021-02-12 18:03

Now that C++ is adding thread_local storage as a language feature, I\'m wondering a few things:

  1. What is the cost of thead_local likely to
2条回答
  •  佛祖请我去吃肉
    2021-02-12 18:55

    Storage space: size of the variable * number of threads, or possibly (sizeof(var) + sizeof(var*)) * number of threads.

    There are two basic ways of implementing thread-local storage:

    1. Using some sort of system call that gets information about the current kernel thread. Sloooow.

    2. Using some pointer, probably in a processor register, that is set properly at every thread context switch by the kernel - at the same time as all the other registers. Cheap.

    On intel platforms, variant 2 is usually implemented via some segment register (FS or GS, I don't remember). Both GCC and MSVC support this. Access times are therefore about as fast as for global variables.

    It is also possible, but I haven't seen it yet in practice, for this to be implemented via existing library functions like pthread_getspecific. Performance would then be like 1. or 2., plus library call overhead. Keep in mind that variant 2. + library call overhead is still a lot faster than a kernel call.

提交回复
热议问题