Skip to content

Instantly share code, notes, and snippets.

@tiandiao123
Created December 21, 2022 22:42
Show Gist options
  • Save tiandiao123/d6d1455987e203dc829547123808993a to your computer and use it in GitHub Desktop.
Save tiandiao123/d6d1455987e203dc829547123808993a to your computer and use it in GitHub Desktop.
#include <iostream>
#include <cmath>
#include <thread>
#include <future>
#include <functional>
using namespace std;
int f(int x, int y){
return std::pow(x, y);
}
void task_lambda(){
std::packaged_task<int(int, int)> task([](int a, int b){
return std::pow(a, b);
});
std::future<int> result = task.get_future();
task(2, 9);
std::cout << "task lambda: " << result.get() << endl;
}
void task_bind(){
std::packaged_task<int()> task(std::bind(f, 2, 11));
std::future<int> result = task.get_future();
task();
std::cout << "task bind " << result.get() << endl;
}
void task_thread(){
std::packaged_task<int(int, int)> task(f);
std::future<int> res = task.get_future();
std::thread rh(std::move(task), 2, 10);
rh.join();
std::cout <<"task thread : " << res.get() << endl;
}
int main(){
task_lambda();
task_bind();
task_thread();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment