- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我必须使用一个 extern 库,它提供了很多免费的功能,可以完成很多网络工作。不幸的是,这个库不是很安全,它恰好永远(或至少很长一段时间)陷入其中一些功能。这对我来说不是一个选择,所以如果通话时间太长,我想中断通话。
看看C++: How to implement a timeout for an arbitrary function call?和 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;
}
像魅力一样编译和工作。然而,问题是,我有很多具有不同参数和返回值的函数,并且为每种情况编写上面的代码对我来说似乎是乏味和多余的工作。所以我想把它包装在一个函数中:
template <class t> t timeout(bindparam<t> &bind /* read below */, 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");
}
我的想法是我可以运行关键函数
try {
int ret = timeout<int>(boost::lambda::bind<int>(&foo, 1, 2), 500);
std::cout << ret << std::endl;
}
catch(std::runtime_error &e) {
std::cerr << e.what() << std::endl;
}
但我不知道该怎么做,甚至不知道是否可行。我可以通过任意 boost::lambda::bind
以某种方式影响我的功能?
更新:
按照建议,我用 boost::packaged_task
试了一下:
template <class T> T timeout(boost::packaged_task<T> &f, long sleep) {
T ret;
boost::thread thrd(boost::lambda::var(ret) = f);
if(thrd.timed_join(boost::posix_time::milliseconds(sleep))) {
return ret;
}
else {
thrd.interrupt();
throw std::runtime_error("timeout");
}
}
但是当我尝试将其用作 timeout<int>(boost::packaged_task<int>(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)’
不是 timeout(boost::packaged_task<int>, int)
几乎完全是我的函数签名 timeout
, 除了 int
将被隐式转换的部分?我做错了什么?
更新 2:
我终于让它工作了,但我不知道我正在做的是否是一个好的方法,因为我发现很难在 boost::packaged_task
上找到任何文档或示例。基本上我使用的是 source code图书馆的。这是我的工作函数:
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");
}
关于c++ - 使用 boost::lambda::bind 的任意函数调用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8992226/
我想要以下内容: void foo( /* something representing a function f */, /* arguments a1, a2, etc. in s
简而言之,我想声明一个这样的特征: trait Test { def test(amount: Int): A[Int] // where A must be a Monad } 这样我就可以
在 GWT 中,如何在 onModuleLoad 方法中插入框架集以及相对嵌套的框架集和框架,以合并许多小程序和其他小部件和 HTML?代码片段是: 公共(public)类 MainEntryPoin
这个问题在这里已经有了答案: How do I best simulate an arbitrary univariate random variate using its probability
我对java相当陌生,并且习惯于枚举本质上只不过是一个命名的整数列表。 现在我正在编写一个实现,其中父类有几个采用枚举值作为参数的方法。枚举将在子类中定义,并且会略有不同。由于枚举基本上看起来像类,所
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 6 年前。 Improve this ques
想象一下 6-7 台服务器的设置都完全相同Java 版本“1.6.0_18”OpenJDK 运行时环境 (IcedTea6 1.8) (fedora-36.b18.fc11-i386)OpenJDK
这个问题在这里已经有了答案: What are some uses of template template parameters? (10 个答案) 关闭 4 年前。 我有一个根据策略舍入值的函数
我正在寻找如何在 Java 中给定一个 Async CompletableFutures 列表,以便前 N 个中的任何一个成功完成或失败。除非没有 N 次成功,否则忽略任何失败。 有这方面的例子吗?
我面临的问题是项目已经使用集群编程来分配任务。 if (cluster.isMaster) { // Fork workers. for (var i = 0; i { }); } el
我正在为 Luxology modo(3D 和 VFX 应用程序)编写脚本,该脚本使用 python 作为脚本语言。在我的脚本中的某个位置,我正在读取从其他应用程序输出的文本文件,并从该文本文件的行创
这个问题在这里已经有了答案: Fast arbitrary distribution random sampling (inverse transform sampling) (5 个答案) 关闭
我只是遇到了一个问题,我有一个结构数组,例如 package main import "log" type Planet struct { Name string `json:"
我正在尝试将 class ResponseResult 编码为 json case class ResponseResult (var Code : Int, var
我想将一个矩阵中的一个 block 复制到另一个矩阵的一部分中。要将其与任何类型的 n 维数组一起使用,我需要通过 [] 运算符应用带有偏移量的列表。有办法做到这一点吗? mat_bigger[0:5
我有一个匹配一组数字和字母的正则表达式。但是我希望能够排除任何三个连续的字母。这是为了防止意外形成单词或缩写。 我的表达如下。它还排除了一些类似的字符,如 0、o、O 和 1、i、I、l): ^[2-
根据documentation . 应匹配任何字符,但不匹配重音字符。 mysql> select 'test' regexp 't.st'; +----------------------+ | '
我该如何用 JavaScript 编写这个 if 语句? if(url == "http://www.google.com/" && "*") { ... } * 需要灵活并接受添加到第一个变量上
我知道 cPython 有一个 GIL,因此如果不使用多处理模块,您的脚本就无法在多个内核上运行。但是有什么可以阻止内置功能,例如使用多核进行排序吗?我不了解 cPython 结构,但我想我要问的问题
寻找命令行 gdb 的替代方法来检查 OSX 上的核心转储 - 有没有办法让 Xcode 打开带有调试符号的任意核心转储? 最佳答案 您是否尝试过使用 MachOView 1? 听起来它可能适用于查看
我是一名优秀的程序员,十分优秀!