Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/143.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 嵌套boost::绑定io_服务::post_C++_Multithreading_Boost_Boost Asio - Fatal编程技术网

C++ 嵌套boost::绑定io_服务::post

C++ 嵌套boost::绑定io_服务::post,c++,multithreading,boost,boost-asio,C++,Multithreading,Boost,Boost Asio,我的问题的简短版本: 当我尝试boost::bind io_service::post时,如下所示: boost::bind(&boost::asio_io_service::post, &ios, boost::bind(&MyClass::func, this, arg1, arg2)); error: no matching function for call to ‘bind(<unresolved overloaded function

我的问题的简短版本:

当我尝试boost::bind io_service::post时,如下所示:

boost::bind(&boost::asio_io_service::post, &ios,
        boost::bind(&MyClass::func, this, arg1, arg2));
error: no matching function for call to ‘bind(<unresolved overloaded function type>,
boost::asio::io_service*, boost::_bi::bind_t<void, boost::_mfi::mf2<void, MyClass,
const char*, const char*>, boost::_bi::list3<boost::_bi::value<MyClass*>,
boost::_bi::value<const char*>, boost::_bi::value<const char*> > >)’
我会遇到如下错误:

boost::bind(&boost::asio_io_service::post, &ios,
        boost::bind(&MyClass::func, this, arg1, arg2));
error: no matching function for call to ‘bind(<unresolved overloaded function type>,
boost::asio::io_service*, boost::_bi::bind_t<void, boost::_mfi::mf2<void, MyClass,
const char*, const char*>, boost::_bi::list3<boost::_bi::value<MyClass*>,
boost::_bi::value<const char*>, boost::_bi::value<const char*> > >)’
错误:调用“bind(,
boost::asio::io_服务*,boost::_bi::bind_t)'
我怎样才能解决这个问题

非常简单的测试代码:

我的问题很长:

我正在尝试编写一个通用事件队列类,您可以将不同类型的事件添加到队列中。您可以按类型订阅事件,添加该类型的事件时,将调用订阅的回调函数

事件队列对象可能由来自不同io_服务的不同线程组订阅。使用boost::lockfree:queue或boost::interprocess::message_queue(如果将来是进程间的话),此类的内部队列将是线程安全的。并且订阅的回调函数将需要由其相应的io_服务的post调用,因此尝试使用上面的嵌套boost::bind

这是一个好的可行的设计吗

假设这种方法可行,我想还有一种替代方法是在订阅时传递io_服务,但我在想1)这个类可能在不涉及io_服务的情况下可以使用,2)这个类不需要了解io_服务

谢谢大家


注意:我读过,但感觉与我的问题有点不同。

更新所有解决方案

问题 第一个问题是
post
具有重载,因此需要消除歧义。那太难看了:

boost::bind(
    static_cast<void (boost::asio::io_service::*)
        (
            boost::_bi::protected_bind_t<
                boost::_bi::bind_t<
                    void, 
                    boost::_mfi::mf2<void, MyClass, int, int>,
                    boost::_bi::list3<boost::_bi::value<MyClass *>,
                    boost::_bi::value<int>,
                    boost::_bi::value<int>
                >
            > > const&
        )>(&boost::asio::io_service::post), 
    &ios_, 
    boost::protect(boost::bind(&MyClass::func, this, 7, 42)));
但这或多或少会驳斥内联绑定表达式的初衷


解决方案 或者,您可以使用一个助手函子,它将重载集隐藏在合适的多态
运算符()后面。

其中
海报
帮助程序看起来像

struct poster {
    typedef void result_type;

    poster(boost::asio::io_service& ios) : ios_(ios) {}
    boost::asio::io_service& ios_;

    template<typename F> void operator()(F const& f) const {
        ios_.post(f);
    }

    template<typename F> void operator()(F&& f) const {
        ios_.post(std::move(f));
    }
};
struct海报{
typedef void result_type;
海报(boost::asio::io_服务和ios):ios_(ios){}
boost::asio::io_服务&ios_;
模板无效运算符()(F常量和F)常量{
监督厅员额(f);
}
模板无效运算符()(F&&F)常量{
ios_.post(标准::移动(f));
}
};

当然,您要做的事情很简单:

std::function<void()> f = [this, arg1, arg2]() {
  ios_.post([this, arg1, arg2]() {
    this->func(arg1, arg2);
  });
};
函数f=[this,arg1,arg2](){ ios.post([this,arg1,arg2](){ 此->函数(arg1,arg2); }); }; 现在,您的线程可以调用
f()
,这将做正确的事情


还是我遗漏了什么…

MyClass::func的签名是什么?试试
boost::bind(&boost::asio_io_服务::post,&ios,boost::protect(boost::bind(&MyClass::func,this,arg1,arg2))MyClass::func(const char*arg1,const char*arg2)我得到了类似的
错误:调用“bind(,boost::asio::io_service*,boost::_bi::protected_bind_t)”时没有匹配的函数
boost::bind(&boost::asio::io\u服务::post,&ios,f)错误变为:
调用“bind(,boost::asio::io_service*,boost::function&)”时没有匹配的函数。
我应该担心
?可能是关于io_service::post()?添加了一个非常简单的测试代码:还将arg1和arg2更改为int。哦,如果您不知道,“第二个问题”是嵌套绑定,但您已经正确地将
protect
应用于内部绑定表达式:)结果是
io_service::post()
是一个模板,因此,
&boost::asio::io_service::post
根本不应该编译。是吗?@IgorR不,确实不是。(我认为进行区分并不有趣,因为逻辑上功能模板只是生成一个重载集。当然,出于您所述的原因,我对相关强制转换的可怕演示显然采用了显式模板实例化的地址)。因此,
static_cast(&io_服务::post)
相当于
&io\u服务::post
?我想知道标准是怎么规定的……啊。那会更简单。不,这相当于
&io_service::post
(很可能是你的意思)。很肯定标准对此有规定,否则不同的编译器可能会有不同的意见。另外,
pmfx=&ioservice::post工作。