Namespaces
Variants

std::thread::name_hint

From cppreference.com
 
 
Concurrency support library
Threads
(C++11)
(C++20)
this_thread namespace
(C++11)
(C++11)
(C++11)
Cooperative cancellation
Mutual exclusion
(C++11)
Generic lock management
(C++11)
(C++11)
(C++11)
(C++11)
(C++11)
Condition variables
(C++11)
Semaphores
Latches and Barriers
(C++20)
(C++20)
Futures
(C++11)
(C++11)
(C++11)
(C++11)
Safe Reclamation
Read-Copy-Update Mechanism
(C++26)
Hazard Pointers
Atomic types
(C++11)
(C++20)
Initialization of atomic types
(C++11)(deprecated in C++20)
(C++11)(deprecated in C++20)
Memory ordering
(C++11)(deprecated in C++26)
Free functions for atomic operations
Free functions for atomic flags
 
 
Defined in header <thread>
template< std::same_as<char> T >
class thread::name_hint;
(since C++29)

A thread attribute that suggests a name for the new thread of execution.

When a name_hint object is passed as one of the leading arguments to the constructor of std::thread or std::jthread, the new thread of execution may be given the specified name. Such names are typically displayed by debuggers, system monitors, profilers, and crash-dump analysis tools, making individual threads easier to tell apart.

T can only be char. The name is interpreted as a string in the associated character encoding of T, i.e., the ordinary literal encoding.

The name is a hint: an implementation may ignore it, or truncate it to the platform-specific length limit (e.g., 15 characters on Linux).

name_hint is neither copyable nor movable. It stores a view of the name instead of copying it, so it is intended to be constructed inline in the thread constructor call; the referenced character sequence must remain valid until that constructor returns.

std::jthread provides name_hint as an alias for std::thread::name_hint.

Member functions

constructs the attribute
(public member function)

Deduction guides

template< class T >
name_hint( const T* ) -> name_hint<T>;
(1) (since C++29)
template< class T >
name_hint( std::basic_string<T> ) -> name_hint<T>;
(2) (since C++29)

Notes

name_hint is a class template although T can only be char: specifying the interface in terms of basic_string_view<T> rather than std::string_view keeps it possible to support additional character types in a future revision without changing the interface or breaking the ABI of existing uses.

Feature-test macro Value Std Feature
__cpp_lib_thread_attributes 202606L (C++29) Thread attributes: std::thread::name_hint and std::thread::stack_size_hint

Example

#include <iostream>
#include <thread>

int main()
{
    // Suggest the name "worker" and a 512 KiB stack for the new thread;
    // in a debugger, crash dump, or system monitor the thread shows up as “worker”.
    std::jthread t(std::jthread::name_hint("worker"),
                   std::jthread::stack_size_hint(512 * 1024),
                   [](int n) { std::cout << n << '\n'; },
                   42);
} // “t” is automatically joined

Output:

42

See also

(since C++29)
suggests a stack size for a new thread
(public member class) [edit]
constructs new thread object
(public member function) [edit]
(C++20)
std::thread with support for auto-joining and cancellation
(class) [edit]