简体   繁体   中英

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?

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.

std::thread is used to create, monitor and manipulate new threads ,
std::this_thread is used inside alreay created threads .

You could have provide this_thread as static methods inside a public class inside std::thread but this is a design decision, I'd dare to say that this kind of design is more Java like, where encapsulating the data as a namespace is more C++ - pilosophy tuned kind of design.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM