如何解决使用 boost::future .then() 将延续任务添加到同一个线程池
我是 Boost 的新手。我正在使用 Boost 1.72.0。我正在尝试探索 boost::asio::thread_pool 并根据 documentation 向其发布任务。我尝试按如下方式发布已发布任务的延续:
- 使用 boost::launch::executor 策略:
fut.then(boost::launch::executor,[](boost::future<void> res) {
std::thread::id this_id = std::this_thread::get_id();
std::cout << "\nThread Id:" << this_id;
std::cout << "\nThen Task: Start";
});
在上述情况下,代码崩溃如下。
> ThreadPool_Boost.exe!boost::executors::executor_ref<boost::executors::executor>::submit(boost::detail::nullary_function<void __cdecl(void)> && closure={...}) Line 73 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::executors::executor::submit(boost::detail::run_it<boost::detail::continuation_shared_state<boost::future<void>,void,void <lambda>(boost::future<void>),boost::detail::shared_state<void>>> &&) Line 107 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::detail::future_executor_continuation_shared_state<boost::future<void>,void <lambda>(boost::future<void>)>::launch_continuation() Line 4711 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::detail::shared_state<void>::do_continuation(boost::unique_lock<boost::mutex> & lock={...}) Line 853 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::detail::shared_state_base::mark_finished_internal(boost::unique_lock<boost::mutex> &) Line 345 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::detail::shared_state<void>::mark_finished_with_result_internal(boost::unique_lock<boost::mutex> &) Line 857 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::detail::shared_state<void>::mark_finished_with_result() Line 864 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::detail::task_shared_state<void <lambda>(void),void __cdecl(void)>::do_run() Line 3423 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::detail::task_base_shared_state<void __cdecl(void)>::run() Line 2995 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::packaged_task<void __cdecl(void)>::operator()() Line 3781 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::asio::asio_handler_invoke<boost::packaged_task<void __cdecl(void)>>(boost::packaged_task<void __cdecl(void)> & function,...) Line 70 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost_asio_handler_invoke_helpers::invoke(boost::packaged_task<void __cdecl(void)> &) Line 37 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::asio::system_executor::dispatch(boost::packaged_task<void __cdecl(void)> &&) Line 39 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::asio::detail::work_dispatcher<boost::packaged_task<void __cdecl(void)>>::operator()() Line 59 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::asio::asio_handler_invoke<boost::asio::detail::work_dispatcher<boost::packaged_task<void __cdecl(void)>>>(boost::asio::detail::work_dispatcher<boost::packaged_task<void __cdecl(void)>> & function={...},...) Line 69 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost_asio_handler_invoke_helpers::invoke(boost::asio::detail::work_dispatcher<boost::packaged_task<void __cdecl(void)>> &) Line 37 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::asio::detail::executor_op<boost::asio::detail::work_dispatcher<boost::packaged_task<void __cdecl(void)>>,std::allocator<void>,boost::asio::detail::scheduler_operation>::do_complete(void * owner=0x011a61d8,boost::asio::detail::scheduler_operation * base=0x011af1c8,const boost::system::error_code & __formal={...},unsigned int __formal=0x00000000) Line 70 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::asio::detail::scheduler_operation::complete(void *) Line 40 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::asio::detail::scheduler::do_run_one(boost::asio::detail::conditionally_enabled_mutex::scoped_lock & lock={...},boost::asio::detail::scheduler_thread_info & this_thread={...},const boost::system::error_code & ec={...}) Line 449 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::asio::detail::scheduler::run(boost::system::error_code & ec={...}) Line 200 C++ Symbols loaded.
[Inline Frame] ThreadPool_Boost.exe!boost::asio::thread_pool::thread_function::operator()() Line 33 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::asio::detail::win_thread::func<boost::asio::thread_pool::thread_function>::run() Line 123 C++ Symbols loaded.
ThreadPool_Boost.exe!boost::asio::detail::win_thread_function(void * arg=0x011a65a0) Line 128 C++ Symbols loaded.
ucrtbase.dll!767b38df() Unknown Non-user code. Symbol loading disabled by Include/Exclude setting.
ucrtbase.dll![Frames below may be incorrect and/or missing,no symbols loaded for ucrtbase.dll] Unknown No symbols loaded.
kernel32.dll!77b36359() Unknown Non-user code. Symbol loading disabled by Include/Exclude setting.
ntdll.dll!77d88944() Unknown Non-user code. Symbol loading disabled by Include/Exclude setting.
ntdll.dll!77d88914() Unknown Non-user code. Symbol loading disabled by Include/Exclude setting.
- 使用“then”的变体,我们可以在其中提及执行者:
fut.then(pool.get_executor(),[](boost::future<void> res) { /* <---- This is line 62 */
std::thread::id this_id = std::this_thread::get_id();
std::cout << "\nThread Id:" << this_id;
std::cout << "\nThen Task: Start";
});
上面的代码编译失败,说明如下:
1>C:\Users\shtrived\Desktop\AsyncTask\ThreadPool_Boost\Demo.cpp(62,5): error C2664: 'boost::future<void> boost::future<void>::then<boost::asio::thread_pool::executor_type,main::<lambda_c8a45d8f92e13883b6218f06c9a06a8d>>(Ex &,F &&)': cannot convert argument 1 from 'boost::asio::thread_pool::executor_type' to 'boost::launch'
1> with
1> [
1> Ex=boost::asio::thread_pool::executor_type,1> F=main::<lambda_c8a45d8f92e13883b6218f06c9a06a8d>
1> ]
问题:
- 为什么会出现上述运行时和编译时错误?
- 如何仅将延续正确发布到线程池?
完整示例代码:
#define BOOST_THREAD_PROVIDES_FUTURE
#define BOOST_THREAD_PROVIDES_SIGNATURE_PACKAGED_TASK
#define BOOST_THREAD_PROVIDES_VARIADIC_THREAD
#define BOOST_ERROR_CODE_HEADER_ONLY
#define BOOST_THREAD_PROVIDES_FUTURE_UNWRAP
#define BOOST_THREAD_PROVIDES_FUTURE_CONTINUATION
#define BOOST_RESULT_OF_USE_DECLTYPE
#define BOOST_THREAD_VERSION 4
#define BOOST_THREAD_PROVIDES_EXECUTORS //on including this we start getting build errors
#include <boost/thread.hpp>
#include <boost/thread/future.hpp>
#include<iostream>
#include<thread>
#include<stdexcept> //only for testing
#include<vector>
//#include<boost/asio/executor.hpp>
#include<boost/asio.hpp>
int main()
{
boost::asio::thread_pool pool(1);
//This is the first function that we want to run on the thread pool
auto func = []() {
cout << "\nTask in progress";
std::thread::id this_id = std::this_thread::get_id();
cout << "\nThread Id:" << this_id << " Task 1: Just a cout";
};
//Make a packaged task with the above function
boost::packaged_task<void()> task(std::move(func));
//Get the future from the packaged task
boost::future<void> fut = task.get_future();
//Post the task into the thread pool
boost::asio::post(pool,std::move(task));
/*
* We want to run the continuation in the same thread pool
*/
fut.then(pool.get_executor(),[](boost::future<void> res) {
std::thread::id this_id = std::this_thread::get_id();
std::cout << "\nThread Id:" << this_id;
std::cout << "\nThen Task: Start";
});
pool.join();
cout << "\nJoin Complete";
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。