#include <userver/compiler/thread_local.hpp>
Creates a unique thread-local variable that can be used in a coroutine-safe manner.
Thread-local variables are known to cause issues when used together with userver coroutines:
Thread-local variables created through this class are protected against these issues.
Example usage:
The thread-local variable is value-initialized.
In C++17 mode, or if you need to initialize the variable with some arguments, the ThreadLocal should be passed a capture-less lambda that constructs the variable. Example:
Once acquired through Use, the reference to the thread-local variable should not be returned or otherwise escape the scope of the ThreadLocalScope object. An example of buggy code:
Do not store a reference to the thread-local object in a separate variable:
Until the variable name goes out of scope, userver engine synchronization primitives and clients (web or db) should not be used.
Definition at line 138 of file thread_local.hpp.
Public Member Functions | |
USERVER_IMPL_CONSTEVAL | ThreadLocal (Factory factory) |
ThreadLocalScope< VariableType > | Use () |
|
inline |
Definition at line 144 of file thread_local.hpp.
|
inline |
Definition at line 146 of file thread_local.hpp.
|
inline |
Definition at line 149 of file thread_local.hpp.