Is thread :: id used anywhere in the C ++ standard library?

std::thread::get_id()

gives you a specific implementation value that uniquely identifies a given stream, but what is interesting to me is that there is a dedicated type for that thread::id

, is this type used somewhere in the standard library?

thread::id

Is A used somewhere or in any interface you know of? AFAIK this type is used nowhere, so it looks like something completely useless at the moment.

+3


source to share


3 answers


The purpose of such user-defined types is to make it easier for developers to implement.

In many cases, you will be embedding C ++ streams on top of existing codebases, OS systems, and the like. They can have different types of thread identification.

With this type, a C ++ std code implementation is more likely to be able to directly elicit the underlying stream identification value, or with minimal modification.



Knowing which stream you are using is very useful in many situations from the client side, and implementing it without an ID from the system is tricky.

std::thread::id

can be sorted, compared (in a fully ordered manner with reasonable equality) and std::hash

ed, all of which are useful with the library std

. They can be copied (trivially) and constructed with no arguments (for an id that does not represent a thread). They can be converted to a string through ostream <<

with the only guarantee that the resulting string will never be the same, except for the two ==

ids.

Any operations on them other than this are undefined. But an implementation could do thread_id

basically an unsigned integer pointer or index in an array, or one of many different base implementations. However, an implementation that accesses such information does something completely implementation-dependent.

+10


source


Is thread :: id used in the C ++ Standard Library?

No, thread::id

it is not used in the C ++ Standard Library interface.

It can be used when implementing one of the recursive mutexes, but that will be an implementation detail. I don't know if any implementation is currently using.

AFAIK this type is used nowhere, so it looks completely useless at the moment.

There are several other types in std :: library that are "useless" by this definition:

  • list
  • install
  • multiset
  • Map
  • MultiMap
  • unordered_set
  • unordered_multimap
  • array
  • atomic
  • BitSet
  • complex
  • condition_variable
  • condition_variable_any
  • forward_list
  • fstream
  • reverse_iterator
  • move_iterator
  • mutex
  • queues
  • Stack
  • regular expression
  • flow

This is not an exhaustive list.


Somewhat reluctant to update

I AM:

Is your question: what is the motivating use case for thread::id

?



user2485710:

yes it sounds right, perhaps with a special emphasis on the standard library.

thread::id

sometimes used to map a stream to attributes, or vice versa. For example, you can implement "named streams" by linking a std::string

to std::thread::id

c std::map

. When a thread of execution registers or throws an exception or has some observable event, the thread name can be found to generate a message for the log, error message, etc. to provide better context. For example, streams can have suggestive names such as "database server" or "table updater".

thread::id

more convenient to use than thread

this application, as thread

it is usually required elsewhere to manage the connection.

Another use for thread::id

is to determine if the current thread executing a function is the same thread as the last thread that performed the same function. I've seen this technique used in implementation recursive_mutex::lock()

. For example, if the mutex is locked and this_thread::get_id() == stored_id

, then the increment lock counts.

As for "focus on the C ++ standard library", 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

it is not used in the C ++ Standard Library interface.

There are many, many types in std :: lib that are not part of the API of the other parts of std :: lib. thread::id

was not standardized because it was needed in the API of other parts of the library. It has been standardized because it is standardized std::thread

, it thread::id

is a natural part of the library std::thread

and because it is useful for use cases like the ones mentioned above.

The key difference between thread

and thread::id

is what thread

maintains unique ownership of the thread of execution. Thus, it thread

is a type of movement. This is very similar to unique_ptr

. For join()

you can only use one std::thread

. On the contrary, thread::id

it is just a "name" for thread

. Names can be copied and matched. They are not used for ownership, only for identification.

This separation of concerns (the privilege of merging with identity) becomes more apparent in a language that supports both floatable and copyable types.

+4


source


AFAIK this type is not used anywhere, so it looks completely useless now.

A thread::id

implements relational operators and hashing support. This allows you, the user, to have them as keys in associative and unordered containers.

+2


source







All Articles