Passing parameters to a thread function

Source: Internet
Author: User
Tags sprintf

Passing parameters to a thread function is done when the thread object is constructed. But remember, by default, parameters are copied to the inside of the thread, even if the reference is used in the function. For example

void f (int i,std::string const &s) Std::thread T (f,3, "Hello");</span>
in the above code, the second parameter of function f is std::string, which is passed Char const * converted to string.

When you use the pointer to point to an automatic variable, pay special attention to:

<span style= "FONT-SIZE:14PX;" >void f (int i, std::string const& s), void oops (int some_param) {char buffer[1024];sprintf (buffer, "%i", Some_param ); Std::thread t (f,3,buffer); T.detach ();} </span>
in this case, the pointer points to the local variable buffer, which is then passed to the newly created thread. It is likely that the function oops has been terminated, but buffer has not yet been converted to std::string, which is already destroyed by buffer. The workaround is to convert before passing:

<span style= "FONT-SIZE:14PX;" >void f (int i, std::string const& s), void oops (int some_param) {char buffer[1024];sprintf (buffer, "%i", Some_param ); Std::thread t (f,3,std::string (buffer)); T.detach ();} </span>
This is the implicit conversion that relies on buffer to std::string, and then as a function parameter

The opposite scenario may occur: the thread copies the object instance, but you want to pass the reference. When you use a reference pass parameter, the thread updates the data:

<span style= "FONT-SIZE:14PX;" >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);} </span>
Although Update_data_for_widget expects the second argument to be passed with a reference, the Std::thread constructor does not know that this is the function's argument being copied. When Update_data_for_widget is called, a reference to the copy data is passed, not a reference to data. When the thread terminates, the copy within the thread is refactored, but the function Process_widget_data passes the data that is not updated. For people who are familiar with Std:bind, there is a quick way to think of the solution: you need to use STD::REF packaging parameters. You need to have a thread created in the form:

<span style= "FONT-SIZE:14PX;" >std::thread T (update_data_for_widget,w,std::ref (data));</span>
If you are familiar with Std::bind, the syntax for parameter passing is easy to understand. Because Std::thread constructors and std::bind all use the same principle. This means that you can pass a pointer to a member function as a function parameter, if you use an object pointer as the first argument.

<span style= "FONT-SIZE:14PX;" >class x{public:void do_lengthy_work ();}; X My_x;std::thread T (&x::d o_length_work,&my_x);</span>
The above code calls My_x.do_lengthy_work () on the new thread because the address of the my_x is the object pointer. You can also provide parameters such as a member function call: The third parameter of Std::thread will be the first parameter of the member function.

Another case is that the function parameter object cannot be copied, only its ownership (for example, the auto_ptr pointer in the STL) can be transferred. Std::unique_ptr is an example of this. The Std::unique pointer can point to only one object at a time, and when the pointer is refactored, the object is also refactored. Transfer ownership (like auto_ptr) when assigning values. When used, move is automatically called when the object is a temporary object, and you must call move when it is a variable.

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));

Passing parameters to a thread function

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.