Is getting the thread ID expensive in terms of performance?

后端 未结 3 1653
逝去的感伤
逝去的感伤 2021-01-13 04:05

I need to access the thread ID from threads that I don\'t control (it\'s in an asynchronous callback function, and it get called from a set of different threads).

I

相关标签:
3条回答
  • 2021-01-13 04:48

    According to MSDN (DevDiv#1039430), prior to VS2015

    this_thread::get_id() was unnecessarily slow

    Just how slow they don't say.

    0 讨论(0)
  • 2021-01-13 04:54

    Windows stores all the thread specific information in the so called TEB. In x86 the fs register points to the start of this structure, in x64 it is the gs register.

    In x86 windows the thread id is stored at FS:[0x24], which presumably should be rather cheap to access. Storing the information in thread local storage involves one extra indirection (we get the address of the TLS from the TEB), so it's basically the same as your handrolled private cache - just less work for you.

    0 讨论(0)
  • 2021-01-13 04:57

    Why not using thread local storage? http://www.boost.org/doc/libs/1_35_0/doc/html/thread/thread_local_storage.html

    0 讨论(0)
提交回复
热议问题