[c++11 Concurrent Programming] 03 passing parameters to threads

Source: Internet
Author: User

We can pass parameters to threads through the Std::thread constructor, but by default, copies of these parameters are passed to the thread, even if the arguments are declared as references:

void f (int i,std::string const& s); Std::thread t (f,3, "Hello");
As shown in the previous example, a thread is created to associate to T, and it calls F (3, "Hello"), although the second parameter of F is std::string, but actually the literal hello is still passed to the thread internally with the Char const * type. It is then converted back to std::string within the new thread context.
Thus, if we were to pass a temporary variable to the thread, the new thread would have dropped the function that had this buffer before converting it to std::string, which would cause a new thread to become problematic. The solution to this problem is to convert the buffer type to std::string when it is passed to the Std::thread constructor:

void f (int i,std::string const& s), void not_oops (int some_param) {char buffer[1024];sprintf (buffer, "%i", Some_param ); Std::thread t (f,3,std::string (buffer)); T.detach ();}
Another scenario is that we want to pass a reference to a struct instance to a thread, and the new thread updates and modifies the structure during its execution:

void Update_data_for_widget (widget_id w,widget_data& data), void Oops_again (widget_id w) {Widget_data data;std:: Thread T (update_data_for_widget,w,data);d isplay_status (); T.join ();p rocess_widget_data (data);}
Update_data_for_widget expects the second argument to be passed to the new thread as a reference, but the Std::thread constructor does not know the requirement, it simply copies the structure and passes it on to the new thread. When Update_data_for_widget executes, it passes the copy reference to itself, not the reference to the external data instance, which results in the end of the thread execution, just modifying the copy of the data, without any change in the data itself.
In this case, we need to use STD::REF to make the reference to data properly passed to Update_data_for_widget.
Std::thread T (update_data_for_widget,w, STD::REF (data));
Another situation is that some parameters cannot be copied but can be moved (move), the data in the object is transferred to another object, the data in the previous object will be emptied. For example, std::unique_ptr, this type is used to manage dynamically allocated objects. At the same time, only one instance of Std::unique_ptr can point to a specific object, and when the pointer is destroyed, the object it points to will also be destroyed. The move constructor and move assignment operators allow us to pass ownership of an object between Std::unique_ptr instances.
The following example uses Std::move to pass ownership of a dynamic object to a thread:

void Process_big_object (std::unique_ptr<big_object>);std::unique_ptr<big_object> p (new Big_object);p- >prepare_data; Std::thread t (Process_big_object,std::move (p));
The Std::thread constructor calls Std::move (p) so that the ownership of the big_objects is passed to the inside of the newly created thread and is then passed to the Process_big_object method.

Removable (movable) but non-copy (copyable) allows us to guarantee that at the same time, only one object is associated with a particular thread, and we can pass ownership of the thread between objects.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

[c++11 Concurrent Programming] 03 passing parameters to threads

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.