- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
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 发布,我想知道是否有更简单的方法?
最佳答案
关于为什么 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
.
虽然这些解决方案都没有提供一般可重用的解决方案,但它可能是最简单的解决方案。除了提供 asio_handler_invoke
之外,官方支持的解决方案是使用自定义处理程序类型。通过 ADL 可用的功能考虑操作的中间和完成处理程序。这是一个主要基于 detail/wrapped_handler.hpp 的完整示例:
#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--abcd++my_great_function++a--my_great_function--bcd
A slightly easier solution that depends on implementation details, is to adapt detail::wrapped_handler
's Dispatcher
type argument. This approach allows for wrapped_handler
s with adapted Dispatcher
types to be transparently used within the rest of Boost.Asio.
/// @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);
}
这两种 wrap_post
解决方案都可能会引入一定程度的复杂性,与定义的 order of handler invocations 有关。 .
关于c++ - 为什么没有 strand::wrap() 等同于 strand::post()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16240716/
判断这2个相似的Uris实际上相同的标准方法是什么? var a = new Uri("http://sample.com/sample/"); var b = new Uri("http://sam
这个问题在这里已经有了答案: Why does "true" == true show false in JavaScript? (5 个答案) 关闭 5 年前。 可能我很困惑,但我无法理解这个愚蠢
我是一名优秀的程序员,十分优秀!