为什么没有strand::wrap()等价于strand::post()
Why no strand::wrap() equivalent for strand::post()?
strand::wrap()
的行为被定义为创建一个函数,该函数将在调用时执行strand::dispatch()
。我最近在我们的一个应用程序中遇到了一个执行以下序列的错误:
my_great_function(..., s.wrap(a), s.wrap(b));
应用程序保证s.wrap(a)
创建的函子在s.wrap(b)
之前被调用。然而,存在一个竞争条件,即第一个函子在串外部调用,因此延迟调用,而第二个函子则在串内部调用并立即执行。这违反了应用程序对a
先于b
的排序假设,并导致了未定义的行为。
使用strand::post()
而不是strand::dispatch()
是解决这一问题的一种方法,但没有像使用strand.wrap()
那样简单的方法。我可以创建助手函数来发布到链中,我想知道是否有更简单的方法?
关于为什么strand.post
的strand.wrap
等价物不存在的纯粹猜测:
- 我找不到哪里有人正式提出在功能请求中需要
strand.wrap()
等效物的情况 - 基于
strand.wrap()
最常见的用法,它可能根据strand.dispatch()
来实现,以优化组合操作的中间处理程序。用户的完成处理程序可以在满足完成条件后立即调用,而不必为延迟调用发布完成处理程序
最简单的解决方案可能是将链与a
和b
处理程序一起传递到my_great_function
。如果my_great_function
需要处理程序调用的特定顺序,那么让my_great_function
保证顺序似乎是可以接受的,而不是将责任推给调用者,后者可能会忽略必要的顺序。另一方面,如果my_great_function
是相当通用的,处理程序之间需要特定的调用顺序,那么可以考虑以直接或间接暗示排序的结构将处理程序一起传递,例如std::tuple
。
虽然这两种解决方案都不提供通用的可重复使用的解决方案,但它可能是最简单的解决方案。官方支持的解决方案是使用自定义处理程序类型,此外还通过ADL提供asio_handler_invoke
函数,以说明操作的中间和完成处理程序。以下是一个完整的示例,主要基于细节/wrapped_handler.hp:
#include <iostream>
#include <boost/asio.hpp>
/// @brief Custom handler wrapper type that will post into its dispatcher.
template <typename Dispatcher,
typename Handler>
class post_handler
{
public:
typedef void result_type;
post_handler(Dispatcher dispatcher, Handler handler)
: dispatcher_(dispatcher),
handler_(handler)
{}
void operator()()
{
dispatcher_.post(handler_);
}
template <typename Arg1>
void operator()(Arg1 arg1)
{
dispatcher_.post(boost::bind(handler_, arg1));
}
template <typename Arg1, typename Arg2>
void operator()(Arg1 arg1, Arg2 arg2)
{
dispatcher_.post(boost::bind(handler_, arg1, arg2));
}
Dispatcher dispatcher_;
Handler handler_;
};
// Custom invocation hooks for post_handler. These must be declared in
// post_handler's associated namespace for proper resolution.
template <typename Function, typename Dispatcher, typename Handler>
inline void asio_handler_invoke(Function& function,
post_handler<Dispatcher, Handler>* this_handler)
{
this_handler->dispatcher_.post(
boost::asio::detail::rewrapped_handler<Function, Handler>(
function, this_handler->handler_));
}
template <typename Function, typename Dispatcher, typename Handler>
inline void asio_handler_invoke(const Function& function,
post_handler<Dispatcher, Handler>* this_handler)
{
this_handler->dispatcher_.post(
boost::asio::detail::rewrapped_handler<Function, Handler>(
function, this_handler->handler_));
}
/// @brief Factory function used to create handlers that post through the
/// dispatcher.
template <typename Dispatcher, typename Handler>
post_handler<Dispatcher, Handler>
wrap_post(Dispatcher dispatcher, Handler handler)
{
return post_handler<Dispatcher, Handler>(dispatcher, handler);
}
/// @brief Convenience factory function used to wrap handlers created from
/// strand.wrap.
template <typename Dispatcher, typename Handler>
post_handler<Dispatcher,
boost::asio::detail::wrapped_handler<Dispatcher, Handler> >
wrap_post(boost::asio::detail::wrapped_handler<Dispatcher, Handler> handler)
{
return wrap_post(handler.dispatcher_, handler);
}
boost::asio::io_service io_service;
boost::asio::strand strand(io_service);
boost::asio::deadline_timer timer(io_service);
void a() { std::cout << "a" << std::endl; }
void b() { std::cout << "b" << std::endl; }
void c() { std::cout << "c" << std::endl; }
void d() { std::cout << "d" << std::endl; }
void noop() {}
void my_great_function()
{
std::cout << "++my_great_function++" << std::endl;
// Standard dispatch.
strand.dispatch(&a);
// Direct wrapping.
wrap_post(strand, &b)();
// Convenience wrapping.
wrap_post(strand.wrap(&c))();
// ADL hooks.
timer.async_wait(wrap_post(strand.wrap(boost::bind(&d))));
timer.cancel();
std::cout << "--my_great_function--" << std::endl;
}
int main()
{
// Execute my_great_function not within a strand. The noop
// is used to force handler invocation within strand.
io_service.post(&my_great_function);
strand.post(&noop);
io_service.run();
io_service.reset();
// Execute my_great_function within a strand.
std::cout << std::endl;
io_service.post(strand.wrap(&my_great_function));
strand.post(&noop);
io_service.run();
}
它产生以下输出:
++my_great_function++--my_great_function--一bcd++my_great_function++一--my_great_function--bcd
取决于实现细节的一个稍微简单的解决方案是调整detail::wrapped_handler
的Dispatcher
类型参数。这种方法允许在Boost.Asio.的其余部分中透明地使用具有自适应Dispatcher
类型的wrapped_handler
/// @brief Class used to adapter the wrapped_handler's Dispatcher type
/// requirement to post handlers instead of dispatching handlers.
template <typename Dispatcher>
struct post_adapter
{
post_adapter(Dispatcher& dispatcher)
: dispatcher_(dispatcher)
{}
template <typename Handler>
void dispatch(const Handler& handler)
{
dispatcher_.post(handler);
}
Dispatcher dispatcher_;
};
/// @brief Factory function used to create handlers that post through an
/// adapted dispatcher.
template <typename Dispatcher, typename Handler>
boost::asio::detail::wrapped_handler<post_adapter<Dispatcher>, Handler>
wrap_post(Dispatcher& dispatcher, Handler handler)
{
typedef post_adapter<Dispatcher> adapter_type;
return boost::asio::detail::wrapped_handler<
adapter_type, Handler>(adapter_type(dispatcher), handler);
}
/// @brief Convenience factory function used to wrap handlers created from
/// strand.wrap.
template <typename Dispatcher, typename Handler>
boost::asio::detail::wrapped_handler<
post_adapter<Dispatcher>,
boost::asio::detail::wrapped_handler<Dispatcher, Handler> >
wrap_post(boost::asio::detail::wrapped_handler<Dispatcher, Handler> handler)
{
return wrap_post(handler.dispatcher_, handler);
}
两种CCD_ 27解决方案都可能引入关于处理程序调用的定义顺序的复杂度。
- 如何使用curlpp通过POST方法上传文件和json数据
- 使用libcurl提交批量url的正确BING Api POST url是什么
- 命名空间 pqxx 无法识别 - 将 libpqxx 添加到 Windows 上的 Visual Studio Post
- 如何使用boost/beast从HTTP POST请求中解析和提取有效负载?
- 如何使用 curl 将 POST 请求从 python 重写为 C++
- 从简单的 POST 请求中解析异常
- Visual Studio 2017 中的 cpprestapi 问题找不到 POST/GET 常量
- 使用 cpp-httplib 的 POST 请求
- POST Arduino 传感器值通过 Flask API 发送到本地 Sqlite db
- C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)
- C++中的 cURL POST 请求
- C++ Schannel POST 400 错误代码错误请求
- c++curl返回413请求实体过大,但是post大小远小于max大小
- HTTP POST 请求不允许我定义上下文类型
- 如何在c++中使用libcurl发送POST请求并接收它
- cpp libcurl 在 HTTP post 调用中发送 zip 文件,而不使用 multipart\form-da
- 使用 http post 在 FCM 中推送通知
- boost::asio::strand post方法性能
- 为什么没有strand::wrap()等价于strand::post()
- strand.post 和strand.wrap之间的性能差异是什么?