I have to use an extern library providing lots of free functions that do a lot of networking stuff. This library, unfortunately, is not very failsafe, and it happens to get stuck in some of these functions forever (or at least a very long time). This is not an option for me, so I want to interrupt the call if it is taking too long.
Taking a look at C++: How to implement a timeout for an arbitrary function call? and the boost::lambda
library, I came up with this:
#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;
}
Compiles and works like a charm. The problem however is, that I have lots of functions with different parameters and return values, and writing the above for every case seems like tedious and redundant work to me. So I want to wrap it in a function:
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");
}
The idea is that I can run the critical function with
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;
}
but I have no idea how to do this, or whether it is even possible. Can I pass arbitrary boost::lambda::bind
s to my function somehow?
Update:
As suggested, I tried it with 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");
}
}
But when I try to use it as timeout<int>(boost::packaged_task<int>(boost::bind(&foo, 1, 2)), 500);
I get a strange compiler error:
main.cpp: In function ‘int main(int, char**)’:
main.cpp:35: error: no matching function for call to ‘timeout(boost::packaged_task<int>, int)’
Isn't timeout(boost::packaged_task<int>, int)
pretty much exactly my function signature of timeout
, except for the int
part that will be converted implicitly? What am I doing wrong?
Update 2:
I finally got it to work, but I have no idea whether what I am doing is a good way of doing it, because I found it incredibly hard to find any documentation or examples on boost::packaged_task
and basically all I worked with is the source code of the library. Here is my working function:
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");
}
I am not completely happy with it, mainly because it does not work with temporaries, meaning you have to go this way to use it:
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;
}
I would still be very happy if someone more adept with these structures could comment on this.
would this work?