Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6198143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T03:56:44+00:00 2026-05-24T03:56:44+00:00

I’m trying to peek stdin to see if anything is there using pthreads. I

  • 0

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;
}    
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T03:56:45+00:00Added an answer on May 24, 2026 at 3:56 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.