userver: userver/utils/thread_name.hpp Source File
Loading...
Searching...
No Matches
thread_name.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <string>
4#include <string_view>
5
6/// @file userver/utils/thread_name.hpp
7/// @brief Functions for thread name manipulation
8/// @ingroup userver_universal
9
10USERVER_NAMESPACE_BEGIN
11
12namespace utils {
13
14/// @brief Get the name of the current thread
15///
16/// Thread names should only be used for debugging purposes. Thread names aren't
17/// required to be preserved exactly due to the system limit on thread name
18/// length.
19///
20/// @returns the current thread name
21/// @throws std::system_error on error
23
24/// @brief Set the name of the current thread
25///
26/// This function should *only* be used from drivers that create their own
27/// threads! Users should normally rely on the coroutine engine instead of
28/// creating additional threads.
29///
30/// There is a system limit on thread name length, e.g. 15 chars on Linux.
31/// The name is automatically cut to fit the limit.
32///
33/// @param name the new thread name
34/// @throws std::system_error on error
35void SetCurrentThreadName(std::string_view name);
36
37/// @brief RAII helper to run some code with a temporary current thread name
38///
39/// This might be useful for spawning external library threads if no other
40/// customisation points are available. All restrictions for thread name
41/// operations apply.
42///
43/// @note There is a chance of failure during thread name update on scope exit.
44/// No errors will be reported in this case.
46 public:
47 /// @brief Set the name of the current thread in the current scope
48 /// @param name temporary thread name
49 /// @throws std::system_error on error
50 CurrentThreadNameGuard(std::string_view name);
51 ~CurrentThreadNameGuard();
52
53 CurrentThreadNameGuard(const CurrentThreadNameGuard&) = delete;
54 CurrentThreadNameGuard(CurrentThreadNameGuard&&) noexcept = delete;
55 CurrentThreadNameGuard& operator=(const CurrentThreadNameGuard&) = delete;
56 CurrentThreadNameGuard& operator=(CurrentThreadNameGuard&&) noexcept = delete;
57
58 private:
59 std::string prev_name_;
60};
61
62} // namespace utils
63
64USERVER_NAMESPACE_END