Table of contents
1. Ordinary functions with return values act as thread processing functions
3. The use of async and deferred
2. Member functions in packaged classes
3. Packaging lambda expressions
Two: Other sub-threads get the data in the promise of this sub-thread
simple process
The first:
1. Use async : start an asynchronous task, create a thread and execute the thread processing function, and the return value is a future object.
2. Obtain the return value of the thread function through the get() method in the future object
1. Ordinary functions with return values act as thread processing functions
#include <thread>
#include <iostream>
#include <future>
using namespace std;
int ReturnValue()
{
return 100;
}
int main()
{
future<int> t = async(ReturnValue);
cout << t.get() << endl;
return 0;
}
2. Use the member function in the class as the thread processing function [with input value and return value]
#include <thread>
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
class DFTX
{
public:
int ReturnClassOne(int num)
{
cout << "当前线程id:" << this_thread::get_id()<<endl;
num *= 2;
this_thread::sleep_for(chrono::milliseconds(5000));//延迟一下5000毫秒
return num;
}
};
int main()
{
DFTX t1;
auto temp = async(&DFTX::ReturnClassOne, &t1, 10);//DFTX类的成员函数ReturnClassOne,对象是t1,输入10,返回temp;
cout << temp.get() << endl;
return 0;
}
3. The use of async and deferred
async : Create threads and execute thread processing functions
deferred : The thread processing function is delayed until we call the wait and get methods, the essence is that no child thread is created
#include <thread>
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
class DFTX
{
public:
int ReturnClassOne(int num)
{
cout << "当前线程id:" << this_thread::get_id()<<endl;
num *= 2;
this_thread::sleep_for(chrono::milliseconds(5000));//延迟一下5000毫秒
return num;
}
};
int main()
{
DFTX t1;
auto temp = async(launch::async,&DFTX::ReturnClassOne, &t1, 10);//launch::async 创建并调用
cout << temp.get() << endl;
auto temp2 = async(launch::deferred, &DFTX::ReturnClassOne, &t1, 50);//在这里并没有发生调用
cout << temp2.get() << endl;//这里才开始调用线程
return 0;
}
The second type:
Use thread to process thread processing functions with return values
1. Wrap the thread processing function through the class template packaged_task
2. Call get_future through the packaged_task object to obtain the future object, and then obtain the return value of the child thread processing function through the get() method
1. Package common functions
#include <thread>
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
int PPAP()
{
return 100;
}
void packThread()
{
packaged_task<int(void)> PackOne(PPAP);//<int(void)>是指返回值为int的函数
thread t1(ref(PackOne));//用ref做一个转换
t1.join();
cout << PackOne.get_future().get() << endl;//获取值
}
int main()
{
packThread();
return 0;
}
2. Member functions in packaged classes
#include <thread>
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
class DFTX
{
public:
int papap(int num)
{
num *= 2;
return num;
}
};
void packThread()
{
DFTX p1;
packaged_task<int(int)> pack1(bind(&DFTX::papap,&p1,placeholders::_1));
thread t1(ref(pack1),20);
t1.join();
cout << pack1.get_future().get() << endl;
}
int main()
{
packThread();
return 0;
}
3. Packaging lambda expressions
#include <thread>
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
class DFTX
{
public:
int papap(int num)
{
num *= 2;
return num;
}
};
void packThread()
{
packaged_task<int(int)> t1([](int num)
{
num *= 10;
return num;
});
thread p1(ref(t1),10);
p1.join();
cout << t1.get_future().get() << endl;
}
int main()
{
packThread();
return 0;
}
The third type:
Obtain the thread function "return value" through promise.
1: Construct the object through the promise class template, and call set_value to store the value that the function needs to return
2: Obtain the future object through get_future, and then obtain the median value of the thread processing function through the get() method [Once the future object is obtained through get_future, you cannot call the get_future function again to obtain a new future object (unless you create a new one) 】
One: normal function
#include <thread>
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
void PromiseTest(promise<int>& SaveData, int data)
{
data *= 3;//计算出值
SaveData.set_value(data);//存储到SaveData里面
}
int main()
{
promise<int> p1;
thread t1(PromiseTest, ref(p1),15);
t1.join();
cout << p1.get_future().get() << endl;
return 0;
}
Two: Other sub-threads get the data in the promise of this sub-thread
#include <thread>
#include <iostream>
#include <future>
#include <chrono>
using namespace std;
void PromiseTest(promise<int>& SaveData, int data)
{
data *= 3;//计算出值
SaveData.set_value(data);//存储到SaveData里面
}
void otherGet(future<int>& SaveData)
{
auto result = SaveData.get();
cout << result<<endl;
}
int main()
{
promise<int> p1;
thread t1(PromiseTest, ref(p1),15);
t1.join();
int nnn = p1.get_future().get();
cout << nnn << endl;//get只能使用一次,否则出错
promise<int> temp;
temp.set_value(nnn);
auto num = temp.get_future();
thread t2(otherGet, ref(num));
t2.join();
return 0;
}