Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::thread class vs std::this_thread namespace in c++?

When we already have a std::thread class then why do we require std::this_thread namespace?

What are the basic differences between them?

When should i use std::thread class and when std::this_thread namespace?

like image 292
roottraveller Avatar asked Nov 22 '15 10:11

roottraveller


People also ask

What does std :: thread do?

std::thread Threads allow multiple functions to execute concurrently. std::thread objects may also be in the state that does not represent any thread (after default construction, move from, detach, or join), and a thread of execution may not be associated with any thread objects (after detach).

How do you use std threads in C++?

std::thread is the thread class that represents a single thread in C++. To start a thread we simply need to create a new thread object and pass the executing code to be called (i.e, a callable object) into the constructor of the object.

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.

Can STD thread fail?

std::thread::join() is permitted to fail, throwing a std::system_error for no_such_process if the thread is "not valid". Note that the no_such_process case is distinct from a thread that is not joinable (for which the error code is invalid_argument ).


1 Answers

The this_thread namespace groups functions that access the current thread, so when we need do something on the current thread, we do not need access to the thread object of the thread.

The thread class does not provide access for yielding and sleeping, those functions only make sense for the current thread, and can thus be found in the this_thread namespace.

If we wish information about a different thread, we need the thread instance of that thread, if we need to access the current thread, we can always do that through the functions in the this_thread namespace.

The thoughts for using a this_thread namespace has also been explained in the draft of the extension:

this_thread Namespace

Note the use of the this_thread namespace to disambiguate when you are requesting the id for the current thread, vs the id of a child thread. The get_id name for this action remains the same in the interest of reducing the conceptual footprint of the interface. This design also applies to the cancellation_requested function:

std::thread my_child_thread(f);
typedef std::thread::id ID:

ID my_id std::this_thread::get_id();  // The current thread's id
ID your_id my_child_thread.get_id();  // The child   thread's id

bool have_i_been_canceled = std::this_thread::cancellation_requested();  // Current thread's cancellation status
bool have_you_been_canceled = my_child_thread.cancellation_requested();  // Child   thread's cancellation status

http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2007/n2320.html

Adding the functions from the this_thread namespace as static members of the thread class could have been done, but then the get_id function would have to be called something else, to make it clearly distinct from the already existing get_id function of the thread class. In other words my guess is that the C++ team decided on adding the functions to a separate namespace to make it more clear that these functions were reading or manipulating the current thread, something that would not have been equally clear had they simply been added as static members of the thread class.

like image 103
Tommy Andersen Avatar answered Sep 20 '22 23:09

Tommy Andersen