【发布时间】:2020-11-02 20:24:45
【问题描述】:
我有一个 lambda 函数,我想将其发布到提升池。 lambda 函数使用一个通过引用传递的大对象。但是,我似乎无法让这段代码正常运行。
#include <boost/asio.hpp>
#include <iostream>
int main() {
int large_object = 10;
auto f1 = [&large_object](int x) {
std::cout << "this is a very large object that needs passed by ref " << large_object << std::endl;
std::cout << x << std::endl;
std::this_thread::sleep_for(std::chrono::seconds {3});
std::cout << "DONE SLEEPING!" << std::endl;
};
int processor_count = 2; // consider that this CPU has two processors
// Launch the pool with n threads.
boost::asio::thread_pool pool(processor_count);
int x = 2;
// Submit a function to the pool.
boost::asio::post(pool, [x]{f1(x);});
pool.join();
return 0;
}
是否可以将此 lambda 函数发布到提升池?
感谢您的宝贵时间。
【问题讨论】:
标签: c++ multithreading boost boost-asio