The boost thread uses the class member function method with parameters.

Source: Internet
Author: User

Assume that the VZController class has the following member functions: [cpp] void downloadData (std: string filename); Put the downloaded data part in a separate thread to execute the download process, but the file must be stored in the main thread. How can this problem be solved? You can use boost: function and boost: bind to generate a packaging function object, and then bind parameters to the function object. The Code is as follows: [cpp] # include <boost/bind. hpp> # include <boost/function. hpp> # include <boost/thread. hpp>... std: string localFileName = "D: \ temp \ dl. dat "; boost: function <void (std: string)> memberFunctionWrapper (boost: bind (& VZController: downloadData, this, _ 1); boost :: thread downloadThread = new boost: thread (boost: bind (memberFuncti OnWrapper, localFileName);... if you want to pass multiple parameters, you can easily change the definition of the function object. "Boost thread library learning notes" (http://www.cnblogs.com/younes/archive/2010/06/06/1752745.html) also introduced a method to pass parameters to the thread function, but more trouble than this method, I have never tried whether it can be used in member functions of class objects.

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.