[C + + 2011 multi-thread Series one] how to create a thread

Source: Internet
Author: User

/*
Thread.cpp
Demonstrates several ways to create a thread
*/

#include <iostream>
#include <sstream>
#include <functional>
#include <thread>
#include <future>
#include <unordered_map>
#include <vector>
#include <algorithm>

using namespace Std;

Template<typename t> t Stringcat (t &x,t &y)
{
std::cout<< "stringcat start ..." <<endl;
x = = x;
y = = y;

Sleep Specifies the number of milliseconds
Std::this_thread::sleep_for (Std::chrono::milliseconds (1000));

std::cout<< "Stringcat end ..." <<endl;
return x + y;
}

Template<typename T> class Ctheadoperator
{
Public
T operator () (t &x,t &y);
Asynchronously initiating a function call
void Async ();
Create a thread initiate an asynchronous call
void CreateThread ();
void Createthreada ();
The creation thread initiates an asynchronous call and gets the result of the call to the thread
void Getthreadresult ();
The creation thread initiates an asynchronous call and gets the result of the call to the thread
void Getthreadresult_lamba ();
};

Template<typename t> void Ctheadoperator<t>::createthread ()
{
std::string x = "X", y = "Y";
Auto B = Std::bind (stringcat<std::string>,x,y);
std::cout<< "CreateThread wait stringcat....\n";
Std::thread TP (b);
Tp.join ();
Watch for changes in return values
std::cout<< "No parameters Modified" <<x<< "" <<y<<endl;
}

Template<typename t> void Ctheadoperator<t>::createthreada ()
{
std::string x = "X", y = "Y";
std::cout<< "Createthreada wait stringcat....\n";
Std::thread TP (STRINGCAT<STD::STRING>,STD::REF (x), y);
Tp.join ();
Watch for changes in return values
std::cout<< "reference to pass-through parameters modified" <<x<< "<<y<<endl;
}

Template<typename t> void Ctheadoperator<t>::getthreadresult ()
{
std::string x = "X", y = "Y";
std::p ackaged_task<std::string (std::string,std::string) > Task (Std::bind (stringcat<std::string>,x,y) );
std::future<std::string> result = Task.get_future ();

std::cout<< "Getthreadresult wait stringcat....\n";
By reference, the value is not modified after the call is finished
Std::thread TASK_TD (Std::move (Task), Std::ref (x), y);
Output thread Some information, attention to see output chaos, because the concurrent
std::cout<< "Getthreadresult threadid:\n" <<task_td.get_id () <<endl;
Detach thread
if (task_td.joinable ())
{
Task_td.detach ();
}



Watch for changes in return values
Std::cout<<result.get () << ", parameters that refer to the pass-through are not modified" <<x<< "" <<y<<endl;
}

Template<typename t> void Ctheadoperator<t>::getthreadresult_lamba ()
{
std::string x = "X", y = "Y";
Constructs Lamba expression syntax: [] (argument list) {implementation}
std::p ackaged_task<std::string (std::string,std::string) >
Package_task ([] (std::string &x,std::string &y) {
Return Stringcat (x,y);}
);

std::future<std::string> result = Package_task.get_future ();
std::cout<< "Getthreadresult_lamba wait stringcat....\n";
By reference, the value is not modified after the call is finished
Std::thread Task (Std::move (Package_task), Std::ref (x), y);
Task.join ();

Watch for changes in return values
Std::cout<<result.get () << ", parameters that refer to the pass-through are not modified" <<x<< "" <<y<<endl;
}

Template<typename t> void Ctheadoperator<t>::async ()
{
std::string x = "X", y = "Y";
Auto handle = Std::async (std::launch::d eferred,aa<std::string>,std::ref (ss), SSS);
Std::future<std::string> handle;
Asynchronously initiating a function call
Handle = Std::async (Std::launch::async,stringcat<std::string>,std::ref (x), y);

std::cout<< "Async wait stringcat....\n";
Watch for changes in return values
Std::cout<}

Template<typename t> T Ctheadoperator<t>::operator () (T &x,t &y)
{
Return Stringcat (x,y);
}

int main (int argc, char* argv[])
{
Using namespace std::p laceholders; For _1, _2, _3 ...

std::string desc = "";
Desc + = "Example simply describes the difference between creating a thread and making an asynchronous call; \ n";
Desc + = "If the simple launch of an asynchronous call, or the use of std::async relatively simple, intuitive, \ n";
Desc + = "can be passed by reference parameters, but also easy to obtain the results of the asynchronous call; \ n";
std::cout<<desc;

Asynchronously initiating a function call
"-------------------------------------------------\ n \ std::cout<<";
ctheadoperator<std::string> TP;
Tp.async ();

Create a thread initiate an asynchronous call
"-------------------------------------------------\ n \ std::cout<<";
Tp.createthread ();

Create a thread initiate an asynchronous call
"-------------------------------------------------\ n \ std::cout<<";
Tp.createthreada ();

The creation thread initiates an asynchronous call and gets the result of the call to the thread
"-------------------------------------------------\ n \ std::cout<<";
Tp.getthreadresult ();

The creation thread initiates an asynchronous call and gets the result of the call to the thread
"-------------------------------------------------\ n \ std::cout<<";
Tp.getthreadresult_lamba ();

"-------------------------------------------------\ n \ std::cout<<";

System ("pause");

return 0;
}


VS2012 Update 2 Run Results:

The example briefly describes the differences between creating threads and asynchronous invocations in different ways;
If the simple launch of an asynchronous call, or the use of std::async relatively simple, intuitive,
Can be passed by reference parameters, at the same time can easily obtain the results of the asynchronous call;
-------------------------------------------------

Async Wait Stringcat ....
Stringcat start ....
Stringcat end ....
XXYY, referencing the pass-through parameters are modified xx y
-------------------------------------------------

CreateThread wait Stringcat ....
Stringcat start ....
Stringcat end ....
No parameters modified X y
-------------------------------------------------

Createthreada wait Stringcat ....
Stringcat start ....
Stringcat end ....
Parameters referenced for passing through are modified XX y
-------------------------------------------------

Getthreadresult wait Stringcat ....
Getthreadresult Detach ThreadID:
Stringcat start ....
8008
Stringcat end ....
XXYY, references to the pass-through parameters have not been modified X y
-------------------------------------------------

Getthreadresult_lamba wait Stringcat ....
Stringcat start ....
Stringcat end ....
XXYY, references to the pass-through parameters have not been modified X y
-------------------------------------------------

Please press any key to continue ...

Related Article

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.