How to Implement Timeout for Function in C++

Want to Implement Timeout for one function in C

Take a look at man 2 alarm. You can set or disable signals which will be sent to your application after a certain time period elapses.

Timeout Function

Probably this dummy program might help you:

#include <stdio.h>
#include <unistd.h>
#include <sys/time.h>
#include <sys/types.h>

#define WAIT 3

int main ()
{
char name[20] = {0}; // in case of single character input
fd_set input_set;
struct timeval timeout;
int ready_for_reading = 0;
int read_bytes = 0;

/* Empty the FD Set */
FD_ZERO(&input_set );
/* Listen to the input descriptor */
FD_SET(STDIN_FILENO, &input_set);

/* Waiting for some seconds */
timeout.tv_sec = WAIT; // WAIT seconds
timeout.tv_usec = 0; // 0 milliseconds

/* Invitation for the user to write something */
printf("Enter Username: (in %d seconds)\n", WAIT);
printf("Time start now!!!\n");

/* Listening for input stream for any activity */
ready_for_reading = select(1, &input_set, NULL, NULL, &timeout);
/* Here, first parameter is number of FDs in the set,
* second is our FD set for reading,
* third is the FD set in which any write activity needs to updated,
* which is not required in this case.
* Fourth is timeout
*/

if (ready_for_reading == -1) {
/* Some error has occured in input */
printf("Unable to read your input\n");
return -1;
}

if (ready_for_reading) {
read_bytes = read(0, name, 19);
if(name[read_bytes-1]=='\n'){
--read_bytes;
name[read_bytes]='\0';
}
if(read_bytes==0){
printf("You just hit enter\n");
} else {
printf("Read, %d bytes from input : %s \n", read_bytes, name);
}
} else {
printf(" %d Seconds are over - no data input \n", WAIT);
}

return 0;
}

Update:

This is now tested code.

Also, I have taken hints from man for select. This manual already contains a code snippet which is being used to read from the terminal and timeout in 5 seconds in case of no activity.

Just a brief explanation in case the code is not well written enough:

  1. We add the input stream (fd = 1) to the FD set.
  2. We initiate select call to listen to this FD set created for
    any activity.
  3. In case any activity occurs within the timeout period, that is
    read through the read call.
  4. In case there was no activity, timeout occurs.

Hope this helps.

How to implement timeout for function in c++

You can create a separate thread to run the call itself, and wait on a condition variable back in your main thread which will be signalled by the thread doing the call to f once it returns. The trick is to wait on the condition variable with your 1s timeout, so that if the call takes longer than the timeout you will still wake up, know about it, and be able to throw the exception - all in the main thread. Here is the code (live demo here):

#include <iostream>
#include <chrono>
#include <thread>
#include <mutex>
#include <condition_variable>

using namespace std::chrono_literals;

int f()
{
std::this_thread::sleep_for(10s); //change value here to less than 1 second to see Success
return 1;
}

int f_wrapper()
{
std::mutex m;
std::condition_variable cv;
int retValue;

std::thread t([&cv, &retValue]()
{
retValue = f();
cv.notify_one();
});

t.detach();

{
std::unique_lock<std::mutex> l(m);
if(cv.wait_for(l, 1s) == std::cv_status::timeout)
throw std::runtime_error("Timeout");
}

return retValue;
}

int main()
{
bool timedout = false;
try {
f_wrapper();
}
catch(std::runtime_error& e) {
std::cout << e.what() << std::endl;
timedout = true;
}

if(!timedout)
std::cout << "Success" << std::endl;

return 0;
}

C++ Implementing a timeout function template for class methods

As the name suggests, std::invoke_result_t is the result type of applying std::invoke. I highlighted the case that applies to you here:

INVOKE(f, t1, t2, ..., tN) is defined as follows:

...

If f is a pointer to member function of class T:

  • If std::is_base_of<T, std::decay_t<decltype(t1)>>::value is true, then INVOKE(f, t1, t2, ..., tN) is equivalent to (t1.*f)(t2, ..., tN)
  • If t1 does not satisfy the previous items, then INVOKE(f, t1, t2, ..., tN) is equivalent to ((*t1).*f)(t2, ..., tN).

So your call should be:

retVal = run_with_timeout(&ClassOne::func, 20s, this, std::move(param1), std::move(name));

EDIT: I struggled for a good twenty minutes with getting it to actually work. Adding std::move as above OR taking name by const reference (in dummy) makes it compile. I cannot think of a similar transformation for param1. I would be very interested to know the reason behind that error, but at least your immediate problem is solved.

How to implement a timeout in read() function call?

select()
takes 5 parameters, first the highest file descriptor + 1, then a fd_set for read, one for write and one for exceptions. The last parameter is a struct timeval, used for timeout. It return -1 on error, 0 on timeout or the number of file descriptors in the sets that are set.

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>

int main(void)
{
fd_set set;
struct timeval timeout;
int rv;
char buff[100];
int len = 100;
int filedesc = open( "dev/ttyS0", O_RDWR );

FD_ZERO(&set); /* clear the set */
FD_SET(filedesc, &set); /* add our file descriptor to the set */

timeout.tv_sec = 0;
timeout.tv_usec = 10000;

rv = select(filedesc + 1, &set, NULL, NULL, &timeout);
if(rv == -1)
perror("select"); /* an error accured */
else if(rv == 0)
printf("timeout"); /* a timeout occured */
else
read( filedesc, buff, len ); /* there was data to read */
close(filedesc);
}


Related Topics



Leave a reply



Submit