Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a "null" value for std::thread::id?

I'd like to have a class member of type std::thread::id where I can also nullify it and then check whether it has been set.

What is the best way to "nullify" this type and then check whether it has been set?

like image 548
user997112 Avatar asked Mar 02 '16 17:03

user997112


People also ask

What type is std :: thread :: id?

std::thread::id The class thread::id is a lightweight, trivially copyable class that serves as a unique identifier of std::thread and std::jthread (since C++20) objects. Instances of this class may also hold the special distinct value that does not represent any thread.

How do I get thread ID?

In the run() method, we use the currentThread(). getName() method to get the name of the current thread that has invoked the run() method. We use the currentThread(). getId() method to get the id of the current thread that has invoked the run() method.

How do I get the thread name in C++?

The pthread_getname_np() function retrieves the name of the thread. The buffer specified by name must be at least 16 characters in length. The returned thread name will be null terminated in the output buffer. By default, each thread is unnamed.

Which of the following is used to declare the identifier for the thread?

pthread_t is the data type used to uniquely identify a thread.


1 Answers

According to cppreference on std::thread::id (default constructor):

Default-constructs a new thread identifier. The identifier does not represent a thread.

So, you can store and compare against a default constructed std::thread::id.

like image 103
melak47 Avatar answered Oct 09 '22 00:10

melak47