17

Is it possible to set timeout for std::cin? For example, std::cin doesn't receive any data during 10 seconds - it throws an exception or returns an error.

Edited:

And what about timer from Boost library? As far as I know, it is portable library. Is it possible to ask timer of Boost library to throw exceptions after predefined period of time? I guess it can solve this problem.

Lucky Man
  • 1,488
  • 3
  • 19
  • 41

3 Answers3

13

It isn't possible to set a time out for std::cin in a portable way. Even when resorting to non-portable techniques, it isn't entirely trivial to do so: you will need to replace std::cin's stream buffer.

On a UNIX system I would replace the default stream buffer used by std::cin by a custom one which uses file descriptor 0 to read the input. To actually read the input I would use poll() to detect presence of input and set a timeout on this function. Depending on the result of poll() I would either read the available input or fail. To possibly cope with typed characters which aren't forwarded to the file descriptor, yet, it may be reasonable to also turn off the buffering done until a newline is entered.

When using multiple threads you can create a portable filtering stream buffer which uses on thread to read the actual data and another thread to use a timed condition variable waiting either for the first thread to signal that it received data or for the time out to expire. Note that you need to guard against spurious wake-ups to make sure that the timeout is indeed reached when there is no input. This would avoid having to tinker with the actual way data is read from std::cin although it still replaces the stream buffer used by std::cin to make the functionality accessible via this name.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • And what about timer from Boost library? As far as I know, it is portable library. Is it possible to ask timer of Boost library to throw exceptions after predefined period of time? I guess it can solve this problem. – Lucky Man Jan 29 '12 at 12:47
  • When you just try to read from `std::cin` it will block this thread and it won't come back. There isn't anything portable to interrupt a system call as far as I know. – Dietmar Kühl Jan 29 '12 at 12:52
  • OK, Thank you Mr. Dietmar Kühl. – Lucky Man Jan 29 '12 at 13:39
  • I know nothing about file descriptors , so I couldn't understand your sentence *I would replace the default stream buffer used by std::cin by a custom one which uses file descriptor 0 to read the input* . What does filebuffer has to do with file descriptor? , Why did we need to do that?. Also, can you link me to some good article? , Thanks a lot :) – Mr.Anubis Jan 29 '12 at 13:47
  • @Mr.Anubis: File descriptors are the low-level POSIX abstractions for all kinds of streamed input (files, keyboard input, sockets, etc.). The standard input to a POSIX process use file descriptor `0` and this is what `stdin` and `std::cin` are connected to. The default implementation for `std::cin` doesn't support setting a timeout but using the system call `poll()` it is possible to time out if there isn't any event in the specified period. – Dietmar Kühl Jan 29 '12 at 14:30
  • @Mr.Anubis: Good articles? For file descriptors look at a UNIX or POSIX forum; for stream buffer search comp.lang.c++ and ... moderated for stream buffer and James Kanze or me. – Dietmar Kühl Jan 29 '12 at 14:31
  • @LuckyMan Boost does have a way to read from a file-descriptor in a way that interfaces with `io_service`, which can be used for single-threaded asynchronous operations. See [this answer](http://stackoverflow.com/a/2831302/1858225), which unfortunately is not much more than a link to an example. Also, it appears that this particular Boost feature is **not** portable; note the `ifdef`s and the word `posix` sprinkled throughout. – Kyle Strand Jan 20 '16 at 19:41
3

I just figured out how to do that, polling the std::cin file descriptor.

poll function returns 0 if timeout occurs and no event happened, 1 if something happened, and -1 if error happened.

#include <iostream>

#include <signal.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <poll.h>


bool stop = false;

void intHandler(int dummy)
{
    stop = true;
}

std::string readStdIn()
{
    struct pollfd pfd = { STDIN_FILENO, POLLIN, 0 };

    std::string line;
    int ret = 0;
    while(ret == 0)
    {
        ret = poll(&pfd, 1, 1000);  // timeout of 1000ms
        if(ret == 1) // there is something to read
        {
            std::getline(std::cin, line);
        }
        else if(ret == -1)
        {
            std::cout << "Error: " << strerror(errno) << std::endl;
        }
    }
    return line;
}

int main(int argc, char * argv[])
{
    signal(SIGINT, intHandler);
    signal(SIGKILL, intHandler);

    while(!stop)
    {
        std::string line = readStdIn();
        std::cout << "Read: " << line << std::endl;
    }
    std::cout << "gracefully shutdown" << std::endl;
}
Giovani
  • 31
  • 2
  • On Linux, `select()` didn't work for me in combination with `getline(std::cin, ...)`, but `poll()` did. – Murphy Mar 07 '17 at 17:24
  • 1
    poll() is WASPoll in windows, this seems portable: https://stackoverflow.com/a/28947000/1831722 – vesperto Oct 25 '20 at 17:34
0

There was a good answer posted here but the author removed it. It's a solution that worked well for me in the application I was developing. This is the essence of what the person wrote:

// compile: g++ -pthread thisfile.cpp

#include <iostream>
#include <thread>

int main() {
    int x;
    bool inputReceived = false;
    time_t startTime = time(NULL);
    time_t waitTime = 10;

    std::cout << "Enter a number within " << waitTime << " seconds\n";

    // spawn a concurrent thread that waits for input from std::cin
    std::thread t1([&]() {
        std::cin >> x;
        inputReceived = true;
    });
    t1.detach();

    // check the inputReceived flag once every 50ms for 10 seconds
    while (time(NULL) < startTime + waitTime && !inputReceived) {
        std::this_thread::sleep_for(std::chrono::milliseconds(50));
    }
    
    if (inputReceived) {
        std::cout << "x = " << x << "\n";
        return EXIT_SUCCESS;
    }
    std::cout << "timeout\n";
    // TODO: find a way to kill the thread
    return EXIT_FAILURE;
}

Be aware that the thread continues running after the timeout occurs, but it will terminate when the whole program terminates. If this is all you need then you don't need to worry about it.

However, there is no simple way to kill a detached thread. A solution would be to close the input stream, but that's not easy or desirable to do with std::cin. If you're lucky then you're going to use this with an easily closeable stream instead of std::cin. Closing the stream will cause input statement to fail and the the thread will probably just exit with an internal exception, but at least the thread will terminate.

Jeff
  • 1,511
  • 2
  • 14
  • 12