通过 lambda 表达式将函数转发给 std::thread
Forwarding a function to std::thread via a lambda expression
我正在阅读注释 here 以了解示例异步函数实现。
正在尝试编译以下代码
#include <future>
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
#include <type_traits>
int f(int x) {
auto start = std::chrono::system_clock::now();
std::time_t start_time = std::chrono::system_clock::to_time_t(start);
std::cout << "returning " << x << " at " << std::ctime(&start_time) << std::endl;
return x;
}
template<typename Function, typename... Args>
auto async(Function&& function, Args&&... args) {
std::promise<typename std::result_of<Function(Args...)>::type> outer_promise;
auto future = outer_promise.get_future();
auto lambda = [function, promise = std::move(outer_promise)](Args&&... args) mutable {
try {
promise.set_value(function(args...));
} catch (...) {
promise.set_exception(std::current_exception());
}
};
std::thread t0(std::move(lambda), std::forward<Args>(args)...);
t0.detach();
return future;
}
int main() {
auto result1 = async(f, 1);
auto result2 = async(f, 2);
}
我遇到以下编译器错误,我很难理解这些错误。
我想要一些指导,说明为什么这个 function
arg 没有根据编译器正确声明。
In instantiation of 'async(Function&&, Args&& ...)::<lambda(Args&& ...)> mutable [with Function = int (&)(int); Args = {int}]':
22:61: required from 'struct async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]::<lambda(int&&)>'
28:3: required from 'auto async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]'
37:27: required from here
22:88: error: variable 'function' has function type
22:88: error: variable 'function' has function type
In instantiation of 'struct async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]::<lambda(int&&)>':
28:3: required from 'auto async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]'
37:27: required from here
22:18: error: field 'async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]::<lambda(int&&)>::<function capture>' invalidly declared function type
要获得 result_of
的结果类型,您必须访问 type
:
std::promise< typename std::result_of<Function(Args...)>::type > outer_promise;
我正在阅读注释 here 以了解示例异步函数实现。
正在尝试编译以下代码
#include <future>
#include <iostream>
#include <thread>
#include <chrono>
#include <ctime>
#include <type_traits>
int f(int x) {
auto start = std::chrono::system_clock::now();
std::time_t start_time = std::chrono::system_clock::to_time_t(start);
std::cout << "returning " << x << " at " << std::ctime(&start_time) << std::endl;
return x;
}
template<typename Function, typename... Args>
auto async(Function&& function, Args&&... args) {
std::promise<typename std::result_of<Function(Args...)>::type> outer_promise;
auto future = outer_promise.get_future();
auto lambda = [function, promise = std::move(outer_promise)](Args&&... args) mutable {
try {
promise.set_value(function(args...));
} catch (...) {
promise.set_exception(std::current_exception());
}
};
std::thread t0(std::move(lambda), std::forward<Args>(args)...);
t0.detach();
return future;
}
int main() {
auto result1 = async(f, 1);
auto result2 = async(f, 2);
}
我遇到以下编译器错误,我很难理解这些错误。
我想要一些指导,说明为什么这个 function
arg 没有根据编译器正确声明。
In instantiation of 'async(Function&&, Args&& ...)::<lambda(Args&& ...)> mutable [with Function = int (&)(int); Args = {int}]':
22:61: required from 'struct async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]::<lambda(int&&)>'
28:3: required from 'auto async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]'
37:27: required from here
22:88: error: variable 'function' has function type
22:88: error: variable 'function' has function type
In instantiation of 'struct async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]::<lambda(int&&)>':
28:3: required from 'auto async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]'
37:27: required from here
22:18: error: field 'async(Function&&, Args&& ...) [with Function = int (&)(int); Args = {int}]::<lambda(int&&)>::<function capture>' invalidly declared function type
要获得 result_of
的结果类型,您必须访问 type
:
std::promise< typename std::result_of<Function(Args...)>::type > outer_promise;