C++ 使用boost::lambda::bind进行任意函数调用?

C++ 使用boost::lambda::bind进行任意函数调用?,c++,boost,lambda,bind,C++,Boost,Lambda,Bind,我必须使用一个extern库,它提供了很多免费函数,可以做很多网络工作。不幸的是,这个库不是非常安全的,而且它碰巧永远(或者至少很长一段时间)卡在这些函数中。这不是我的选择,所以如果通话时间太长,我想中断通话 查看一下boost::lambda库,我得出以下结论: #include <iostream> #include <boost/thread/thread.hpp> #include <boost/lambda/lambda.hpp> #include

我必须使用一个extern库,它提供了很多免费函数,可以做很多网络工作。不幸的是,这个库不是非常安全的,而且它碰巧永远(或者至少很长一段时间)卡在这些函数中。这不是我的选择,所以如果通话时间太长,我想中断通话

查看一下boost::lambda库,我得出以下结论:

#include <iostream>
#include <boost/thread/thread.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

int foo(int a, int b) {
    boost::this_thread::sleep(boost::posix_time::seconds(2));
    return a+b;
}

int main() {
    int ret;
    boost::thread thrd(boost::lambda::var(ret) = boost::lambda::bind<int>(&foo, 1, 2));
    if(thrd.timed_join(boost::posix_time::seconds(1))) {
        std::cout << ret << std::endl;
    }
    else {
        std::cerr << "Function timed out." << std::endl;
    }
    return 0;
}
但当我尝试将其用作超时(boost::packaged_task(boost::bind(&foo,1,2)),500)我遇到一个奇怪的编译器错误:

main.cpp: In function ‘int main(int, char**)’:
main.cpp:35: error: no matching function for call to ‘timeout(boost::packaged_task<int>, int)’
我对它并不完全满意,主要是因为它不适用于临时人员,这意味着你必须这样使用它:

try {
    boost::packaged_task<int> f(boost::lambda::bind(&foo, 1, 2));
    int sum = timeout<int>(f, 500);
    std::cout << sum << std::endl;
}
catch(std::runtime_error &e) {
    std::cerr << e.what() << std::endl;
}
试试看{
boost::打包的_任务f(boost::lambda::bind(&foo,1,2));
int sum=超时(f,500);
这行吗

template <class T, class F>
T timeout(const F &bind, long sleep) {
    T ret;
    boost::thread thrd(boost::lambda::var(ret) = bind);
    if(thrd.timed_join(boost::posix_time::milliseconds(sleep))) {
        return ret;
    }
    else throw std::runtime_error("timeout");
}
模板
T超时(常数F&bind,长睡眠){
T-ret;
boost::thrd(boost::lambda::var(ret)=bind);
if(thrd.timed_连接(boost::posix_时间::毫秒(睡眠))){
返回ret;
}
else抛出std::runtime_错误(“超时”);
}
这样行吗

template <class T, class F>
T timeout(const F &bind, long sleep) {
    T ret;
    boost::thread thrd(boost::lambda::var(ret) = bind);
    if(thrd.timed_join(boost::posix_time::milliseconds(sleep))) {
        return ret;
    }
    else throw std::runtime_error("timeout");
}
模板
T超时(常数F&bind,长睡眠){
T-ret;
boost::thrd(boost::lambda::var(ret)=bind);
if(thrd.timed_连接(boost::posix_时间::毫秒(睡眠))){
返回ret;
}
else抛出std::runtime_错误(“超时”);
}


为什么不直接使用
boost::packaged_task
?@Mankarse说实话,因为我从来没有听说过它。我会研究一下。谢谢。@Mankarse让它与boost::packaged_task一起使用。谢谢!PS:也许可以把你的评论升级为一个答案,这样我就可以给你评分了?为什么不直接使用
boost::packaged_task
?@Mankarse老实说,因为我从来没有听说过它。我会研究一下。谢谢。@Mankarse让它与boost::packaged_任务一起工作。谢谢!PS:也许可以将您的评论升级为一个答案,这样我就可以给您评分了?我将如何调用此函数?如果函数超时,行为是未定义的。线程将继续运行,并且它将在ally尝试在
ret
中存储一个值,但是在控件离开
timeout
后,对
ret
的引用将不再有效。@Rob Kennedy是的,线程应该在
else
情况下中断。谢谢。您调用它就像yiu在示例中所做的那样。F类型将自动解析。请确保您的bind对象可以是常量,也可以被复制。噢,天哪,有时候我真的看不到眼前的解决方案。谢谢你的帮助,它工作得很好。我该如何调用这个函数?如果函数超时,行为是未定义的。线程将继续运行,它最终会尝试在
ret
中存储一个值,但在c之后Control离开
超时
,对
ret
的引用将不再有效。@Rob Kennedy是的,线程应该在
else
案例中中断。谢谢。你调用它就像yiu在示例中所做的那样。F类型将自动解析。只需确保你的绑定对象可以是const或可以被复制。哦,孩子,有时候我真的没有看到眼前的解决方案。谢谢你的帮助,它工作得很好。
template <class T> T timeout(boost::packaged_task<T> &f, long sleep) {
    boost::thread thrd(boost::lambda::bind(&boost::packaged_task<T>::operator(), &f));
    if(thrd.timed_join(boost::posix_time::milliseconds(sleep))) {
        boost::unique_future<T> ret = f.get_future();
        return ret.get();
    }
    thrd.interrupt();
    throw std::runtime_error("timeout");
}
try {
    boost::packaged_task<int> f(boost::lambda::bind(&foo, 1, 2));
    int sum = timeout<int>(f, 500);
    std::cout << sum << std::endl;
}
catch(std::runtime_error &e) {
    std::cerr << e.what() << std::endl;
}
template <class T, class F>
T timeout(const F &bind, long sleep) {
    T ret;
    boost::thread thrd(boost::lambda::var(ret) = bind);
    if(thrd.timed_join(boost::posix_time::milliseconds(sleep))) {
        return ret;
    }
    else throw std::runtime_error("timeout");
}