如果有一个通用方法接受一些处理程序:
template< typename HandlerType >
void Register( HandlerType && handler )
{
m_handler( std::forward< HandlerType >( handler ) );
}
并且该处理程序将通过 io_service
调用在未来的某个时刻:
void SomeEvent( )
{
// compute someParameter
m_IOService.post( std::bind( m_handler , someParameter ) );
}
Register()
的调用者如何检测通过了用 strand
包裹的东西,如:
m_strand( m_IOService );
// ...
Register( m_strand.wrap( []( /*something*/ ){ /*...*/ } ) );
以及如何SomeEvent()
应该更改以便在这种情况下通过链发布处理程序?
编辑
当我问这个问题时,我没有仔细阅读 io_service::strand::wrap
docs 的麻烦,更具体地说:
(...) Given a function object with the signature:
R f(A1 a1, ... An an);
If this function object is passed to the wrap function like so:
strand.wrap(f);
then the return value is a function object with the signature
void g(A1 a1, ... An an);
that, when invoked, executes code equivalent to:
strand.dispatch(boost::bind(f, a1, ... an));
我所需要的就是这个——我可以声明m_handler
作为适当的std::function<>
并通过 io_service
简单地发布它在SomeEvent()
.
我是看了@Arunmu 的回答后才意识到这一点的,所以我接受了。尽管如此,@Richard Hodges 的回答对 ASIO 的执行程序逻辑以及它在独立版本中的改进方式有一些好处。
最佳答案
如果我清楚地理解了您的要求,那么如果像下面这样实现,您就不必做任何额外的事情(阅读代码中的注释以获得解释):
#include <iostream>
#include <type_traits>
#include <thread>
#include <memory>
#include <asio.hpp>
template <typename Handler>
class GenHandler
{
public:
GenHandler(Handler&& h): hndler_(std::forward<Handler>(h))
{}
template <typename... Args>
void operator()(Args&&... args)
{
std::cout << "GenHandler called" << std::endl;
hndler_();
}
private:
Handler hndler_;
};
template<typename HandlerType>
GenHandler<std::decay_t<HandlerType>> create_handler(HandlerType&& h)
{
return {std::forward<HandlerType>(h)};
}
template <typename Handler>
void SomeEvent(asio::io_service& ios, Handler& h)
{
ios.post([=] ()mutable { h(); });
}
int main() {
asio::io_service ios;
asio::io_service::strand strand{ios};
auto work = std::make_unique<asio::io_service::work>(ios);
std::thread t([&]() { ios.run(); });
// This creates a regular handler which when called by the
// io_context would first execute GenHandler::operator()
// and inside of which it would call the lambda passed below.
auto hndl = create_handler([] {
std::cout << "Regular Handle" << std::endl;
});
SomeEvent(ios, hndl);
///-------- Example 2 ---------////
// This creates a handler just like above, but instead wraps a
// strand handler i.e when GenHandler::operator() gets called
// it will execute the lambda passed to the wrap in the execution context
// of the strand.
auto hndl2 = create_handler(
strand.wrap([] {
std::cout << "Strand handler-depth 2" << std::endl;
}));
// This is a regular strand wrap which is passed to the
// io_service execution context. The lambda passed in the strand::wrap
// would be excuted the execution context of the strand.
auto str_handler = strand.wrap([=]() mutable {
std::cout <<"strand\n";
hndl2();
});
SomeEvent(ios, str_handler);
work.reset();
t.join();
return 0;
}
在第二个示例中,处理程序按以下顺序调用:
io_service
被传递给strand::wrapped_handler
。因此,wrapped_handler
持有的处理程序在 strand 内部执行。hndl2
是GenHandler
持有另一个strand::wrapped_handler
也在链内部调用。- 当
GenHandler::operator()
被调用时,它也会执行持有的strand::wrapped_handler
。这是通过将strand::wrapped_handler
持有的内部处理程序分派(dispatch)给 strand 来完成的。
注意:由于我不清楚的原因,strand::wrap
已被弃用。作者希望人们改用 bind_executor
。
关于c++ - 如何检测处理程序是否为 ASIO strand wrap 并通过 strand 调用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41106393/