Reputation: 9801
std::thread::get_id()
gives you an implementation defined value which uniquely identifies a given thread, but the interesting thing for me is that there is a dedicated type for this, thread::id
, is this type used anywhere in the standard library ?
A thread::id
is used somewhere or in any interface that you know of ? AFAIK this type is used nowhere hence it looks like it's quite useless at the moment .
Upvotes: 3
Views: 828
Reputation: 275190
The purpose of such user defined types is to make things easy for implementors.
In many cases, you will be implementing C++ threads on top of existing code bases, OS systems, or the like. These may have different types of thread identification.
With that type, the C++ std implementation is more likely to be able to expose the underlying thread identification value directly, or with minimal modification.
Knowing what thread you are on is quite useful in many situations from the client side, and implementing it without an id from the system is complex.
std::thread::id
can be sorted, compared (in a totally ordered way, with sensible equality) and std::hash
ed, all of which are useful with the std
library. They can be copied (trivially) and constructed with no arguments (for the id that represents no thread). They can be turned into a string via ostream <<
, with the only guarantee that the resulting string is never the same except for two ==
ids.
Any operations on them beyond that is undefined. But, an implementation could make thread_id
be basically a pointer, or an unsigned integer index in an array, or one of many different underlying implementations. An implementation who accesses such information is doing something completely implementation dependent, however.
Upvotes: 10
Reputation: 218700
Is thread::id used anywhere in the standard C++ library?
No, thread::id
is not used in the interface of the standard C++ library.
It might be used in the implementation of one of the recursive mutexes, but that would be an implementation detail. I do not know if any implementation currently uses it.
AFAIK this type is used nowhere hence it looks like it's quite useless at the moment.
Here are a few of the other types in the std::library that are "useless" by this definition:
This is not an exhaustive list.
Somewhat reluctant update
Me:
Is your question: What is a motivating use case for
thread::id
?
user2485710:
yes, that sounds right, with, maybe, a special focus on the standard library.
thread::id
is sometimes used to map a thread to attributes or vice-versa. For example one might implement "named threads" by associating a std::string
with a std::thread::id
in a std::map
. When a thread of execution logged, or throws an exception, or has some notable event, one could look up the name of the thread to create a message for the log, error message, etc, in order to give better context. For example threads might have suggestive names such as: "database server" or "table updater".
thread::id
is more convenient to use than thread
for this application as thread
is usually needed elsewhere to control joining.
Another use for thread::id
is to detect if the current thread executing a function is the same thread as the last thread that executed that same function. I've seen this technique used in the implementation of recursive_mutex::lock()
. For example, if the mutex is locked and this_thread::get_id() == stored_id
, then increment lock count.
As far as "focus on the standard C++ library" is concerned, I really don't know what that means. If it means: "Used in the interface", then this question has already been answered earlier in this answer, and in other answers:
No,
thread::id
is not used in the interface of the standard C++ library.
There are many, many types in the std::lib that are not part of the API of other parts of the std::lib. thread::id
was not standardized because it was needed in the API of other parts of the library. It was standardized because std::thread
was being standardized, and thread::id
is a natural part of the std::thread
library, and because it is useful for the use cases such as those mentioned above.
The key difference between thread
and thread::id
is that thread
maintains unique ownership of a thread of execution. Thus thread
is a move-only type. This is very analogous to unique_ptr
. Only one std::thread
can be used to join()
. In contrast, a thread::id
is just a "name" for a thread
. Names are copyable and comparable. They aren't used for ownership, only for identification.
This separation of concerns (privilege-to-join vs identification) is made more obvious in a language which supports both move-only and copyable types.
Upvotes: 4
Reputation: 81349
AFAIK this type is used nowhere hence it looks like it's quite useless at the moment .
A thread::id
implements relational operators and hash support. This allows you, the user, to have them as keys in associative and unordered containers.
Upvotes: 2