Concurrent Programming: C++11 thread (Func, Args ...) Creating Threads with class member functions

Source: Internet
Author: User

C++11 is a new standard supported by VS2012, providing a convenient std::thread for concurrent programming.

Examples of Use:

#include <thread>voidThread_func (intArg1,intArg2,float*Arg3) {Arg3= (arg1*1.0)/(Arg1 +arg2); cout<<"arg1/(arg1 + arg2) ="<< Arg3 <<Endl; return;}voidMain () {//Number of Threads    intThreadnum =3    //Dynamic Allocationthread*T; T=NewThread[threadnum]; //Results    float*result = (float*)malloc(threadnum*sizeof(float));  for(inti =0; i < threadnum; i++) {T[i]= Thread (Thread_func, I, I, &Result[i]); }     for(inti =0; i < threadnum; i++){        //T[i].detach ();//The main process is not equal to the child process running outT[i].join ();//main process, etc.    }    //post-processing towards result ...}

When you need to use the class member function ( myclass::thread_func ) to create a child thread, the following code is required:

T[i] = Thread (STD::MEM_FN (&myclass::thread_func), Object, args..);    

If Thread_func is static, you do not have to write object. Otherwise, if the function that the main process is tuned to is also a member of that class, the incoming this refers back to itself.

Concurrent Programming: C++11 thread (Func, Args ...) Create a thread with a class member 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.