[英]Capturing boost::asio::thread_pool in lambda function
I'm trying to capture thread_pool object in a lambda function.我正在尝试在 lambda 函数中捕获 thread_pool 对象。 This lambda function is called inside a thread.
这个 lambda 函数在线程内被调用。 Upon this call, it creates(obtains) a new thread with asio::post.
在这个调用中,它使用 asio::post 创建(获取)一个新线程。 However, it throws segmentation fault.
但是,它会引发分段错误。 I tried create weak ptr with shared_ptr<thread_pool> but it didn't work as well.
我尝试使用 shared_ptr<thread_pool> 创建弱 ptr,但效果不佳。 Simple example written below,
下面写一个简单的例子,
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <iostream>
void thread1(std::function<void()> createThread) {
createThread();
}
void thread2() {
cout << "You made it" << std::endl;
}
int main(int argc, char **argv) {
boost::asio::thread_pool pool(std::thread::hardware_concurrency());
std::function<void()> createThread;
createThread = [&pool] () {
boost::asio::post(pool, boost::bind(thread2));
return true;
};
boost::asio::post(pool, boost::bind(thread1, createThread));
pool.join();
}
It works if I create another thread_pool object inside the lambda function.如果我在 lambda 函数中创建另一个 thread_pool 对象,它会起作用。 However, this is not the right way to do this.
但是,这不是执行此操作的正确方法。 Therefore, I am open for your suggestions.
因此,我愿意接受您的建议。
Edit: Added libraries to code snippet and removed while loop.编辑:将库添加到代码片段并删除了 while 循环。
I'd simplify:我会简化:
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <iostream>
void thread1(std::function<void()> createThread) {
createThread();
while (true) {
std::cout << "Sleeping" << std::endl;
sleep(1);
}
}
void thread2() { std::cout << "You made it" << std::endl; }
int main() {
boost::asio::thread_pool pool;
post(pool,
boost::bind(thread1, [&pool]() { post(pool, boost::bind(thread2)); }));
pool.join();
}
Note the endl
that forces stdout to flush, which helps getting results you can expect.请注意强制标准输出刷新的
endl
,这有助于获得您可以预期的结果。
There's a code smell with:有一种代码气味:
bind
expressionsbind
表达式createThread
doesn't (create a thread) createThread
没有(创建线程) Applying these:应用这些:
#include <boost/asio.hpp>
#include <boost/bind/bind.hpp>
#include <iostream>
using Executor = boost::asio::thread_pool::executor_type;
void task_loop(Executor ex, std::function<void()> task) {
while (true) {
post(ex, task);
sleep(1);
}
}
void task_function() { std::cout << "Task executes" << std::endl; }
int main() {
boost::asio::thread_pool pool;
post(pool, boost::bind(task_loop, pool.get_executor(), task_function));
pool.join();
}
Prints each second:每秒打印:
Task executes
Task executes
...
Is this one what you look for?这是你要找的吗? :
:
typedef std::unique_ptr<boost::asio::io_service::work> work_ptr;
std::atomic<bool> closeFlag(false);
int main(int argc, char** argv) {
boost::asio::io_service service;
// keep the workers occupied
work_ptr work(new boost::asio::io_service::work(service));
boost::thread_group workers;
for(size_t i = 0; i < std::thread::hardware_concurrency(); ++i) {
workers.create_thread([&service]() {
service.run();
});
}
service.post([] { std::cout << "You made first job"; });
service.post([] { std::cout << "You made second job"; });
while(!closeFlag) {
boost::this_thread::sleep(boost::posix_time::milliseconds(100));
}
service.stop();
work.reset(); // destroy work object: signals end of work
workers.join_all(); // wait for all worker threads to finish
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.