Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Peek stdin using pthreads

I'm trying to peek stdin to see if anything is there using pthreads. I (think I) need to do this because if there is nothing in std in, stream access functions will block for input.

I feel the way to do this is to fire off a pthread that checks stdin, and sleep(1) and see if the thread found anything.

Here's what I have so far. If nothing is piped into the program then it will sleep as expected, but if something is in stdin the thread never gets fired.

#include <iostream>
#include <pthread.h>
#include <stdlib.h>

using namespace std;

void *checkStdIn(void *d){
    char c = '\0';
    c = cin.peek();
    if (c){
        cout << c << endl;
    }
}

int main(int argc, char *argv[]){

    pthread_t thread;
    int rc;
    rc = pthread_create(&thread, NULL, checkStdIn, NULL);
    if (rc){
        cerr << "Error no. " << rc << endl;
        exit(EXIT_FAILURE);
    }
    sleep(2); 

    return 0;
}    
like image 500
zero_dev Avatar asked Jun 27 '26 07:06

zero_dev


1 Answers

You don't need pthreads for that and you can use select(2) or poll(2) to known if you can peek the stdin without blocking your application. For that I coded the my_peek() function which you just need to pass the number of seconds you want to wait for the input (you can even pass 0 if you don't want to wait):

/* According to POSIX.1-2001 */
#include <sys/select.h>

/* According to earlier standards */
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>

#include <iostream>

int
my_peek(unsigned int nsecs)
{
    struct timeval timeout;
    fd_set rfds;
    int fd;

    // stdin file descriptor is 0
    fd = 0;

    timeout.tv_sec = nsecs;
    timeout.tv_usec = 0;

    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);

    if (select(fd + 1, &rfds, NULL, NULL, &timeout) > 0)
        return std::cin.peek();
    return -1;
}

int
main(void)
{
    int peek;

    peek = my_peek(2);
    if (peek != -1) {
        std::cout << "we could peek without freezing" << std::endl;
        std::cout << "my_peek() returned " << peek << std::endl;
    } else {
        std::cout << "we could not peek without freezing" << std::endl;
    }

    return 0;
}

Please note that it is BAD to rely on select(2) to tell if there is data in the cin object or stdin FILE structure because, as Nemo stated, they are BUFFERED. The best thing to do is avoid "cin" in this example using read(2). The improved version of my_peek() would look like:

int
my_peek(unsigned int nsecs)
{
    struct timeval timeout;
    fd_set rfds;
    int fd;
    unsigned char c;

    // stdin file descriptor is 0
    fd = 0;

    timeout.tv_sec = nsecs;
    timeout.tv_usec = 0;

    FD_ZERO(&rfds);
    FD_SET(fd, &rfds);

    if (select(fd + 1, &rfds, NULL, NULL, &timeout) <= 0)
        return -1;
    if (read(fd, &c, 1) != 1)
        return -1;
    return static_cast<int>(c); /* or "return (int)c" for C-only programs */
}

For more information please check the select(2) manual page at http://linux.die.net/man/2/select.

PS: You could try to rely on the value returned by the std::cin.rdbuf()->in_avail() as explained in the cpluplus site http://www.cplusplus.com/reference/iostream/streambuf/in_avail/, or even in the readsome() method of the istream class, but they usually depend on the FILE buffer which is not exposed in the GNU C++ library. Don't do it or you might fail.

like image 171
Fernando Silveira Avatar answered Jun 28 '26 21:06

Fernando Silveira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!