Is it expected that use of boost::thread_specific_ptr<>::get() be slow? Any work arounds?

蓝咒 提交于 2019-12-03 05:35:23

thread_specific_ptr uses pthread_setspecific/pthread_getspecific for POSIX systems which is not the fastest possible.

If you are on a POSIX system, you can use the __thread storage specifier. However, it can only be used with initializers that are constant expressions e.g gcc's __thread

For Windows, a similar specifier is _declspec(thread).

Obtaining thread local data will most probably involve a system call. System calls jump to an interrupt vector as well as now having to read kernel memory. All this kills the cache.

For this reason reading thread local data can much longer than a normal variable read. For this reason is may well be a good idea to cache thread local data some local variable an not make frequent accesses to thread local storage.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!