我尝试使用打包的任务来制作任务。
我使用成员协程,该协程的参数包括字符串类型的成员和指向类的共享指针。
然后我会将任务的未来放在容器中,以便与wait_for_any一起使用。
然后将任务发布到asio ioservice。
这是协程的原型:
std::string Execute(boost::asio::yield_context, std::string, std::map<std::string, boost::shared_ptr<HTTPResponse>>&);
这就是我编写打包任务的方式,请注意协程执行是另一个类的成员,该类的实例使用map中的shared_pointer保存,并且在这里我取消引用此映射共享指针以获取指向实例的指针(代替指针)成员函数所需的“ this””:
boost::packaged_task<std::string(boost::asio::yield_context, std::string, std::map<std::string, boost::shared_ptr<HTTPResponse>>&)> task(boost::bind(&HTTPRequest::Execute, mClientRequestsVariables[m_HttpClient_request_name].get(),boost::placeholders::_1, m_HttpClient_request_name, Get_mHTTPClient_Responses_Map()));
这就是我其余的写法:
boost::future<std::string> fut = (task.get_future());
mPendingData.push_back(std::move(fut)); // C++11 possible: (std::move(fut) when fut is a unique_future);
mIos.post(std::move(task));
它给了我下一个错误:
Error C2280 'boost::packaged_task<std::string (boost::asio::yield_context,std::string,std::map<std::string,boost::shared_ptr<HTTPResponse>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &)>::packaged_task(const boost::packaged_task<std::string (boost::asio::yield_context,std::string,std::map<_Kty,_Ty,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>> &)> &)': attempting to reference a deleted function
Error C2338 CompletionHandler type requirements not met
Error C2064 term does not evaluate to a function taking 0 arguments
我该怎么办?
更新:
我阅读了有关packaged_task的一些答案,并尝试将代码更改为:
typedef boost::packaged_task<std::string(boost::asio::yield_context, std::string, boost::shared_ptr<std::map<std::string, boost::shared_ptr<HTTPResponse>>>)> task_t;
//typedef boost::packaged_task<std::string()> task_t;
boost::shared_ptr<task_t> task = boost::make_shared<task_t>(boost::bind(&HTTPRequest::Execute, mClientRequestsVariables[m_HttpClient_request_name].get(), boost::placeholders::_1, m_HttpClient_request_name, Get_mHTTPClient_Responses_Map_shared_pointer()));
boost::future<std::string> fut = (task->get_future());
mPendingData.push_back(std::move(fut)); // C++11 possible: (std::move(fut) when fut is a unique_future);
并注释了这一行:
//????????mIos.post(boost::bind(&task_t::operator(), task));
并编译代码。
但是当我取消注释此行时:
mIos.post(boost::bind(&task_t::operator(), task));
发生此错误:
Error C2440 'return': cannot convert from 'void (__cdecl &)(boost::asio::yield_context,std::string,boost::shared_ptr<std::map<std::string,boost::shared_ptr<HTTPResponse>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>>)' to 'void (&)(boost::asio::yield_context,std::string,boost::shared_ptr<std::map<std::string,boost::shared_ptr<HTTPResponse>,std::less<_Kty>,std::allocator<std::pair<const _Kty,_Ty>>>>)'
io_service帖子出什么问题了?