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;
}
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.
Your sleep(2)
is worthless because it does not guarantee that your thread will finish within 2 microseconds prior to program termination. You need to implement pthread_join(thread, NULL);
to wait for the thread to finish. See here for a good pthread example.
Also, cin.peek() will block waiting for input. That is how it is designed. See here for an example of cin.peek.
Edit: Bawh, ninja'd by Fernando :)
Okay, so I'm not 100% sure on what the best answer will be for you because I can't tell what you want your program to eventually do.
First and foremost, this is not the kind of problem that should be solved using threads. Threads are not the catch-all solution to everything and generally have huge overhead compared to other solutions. Because you're already using pthreads
, I'll assume Windows compatibility isn't an issue.
The first step is to disable canonical mode, which will allow you to gett characters without having to wait for enter
. If you were 100% sure stdin
won't ever be a terminal, you can skip this step.
#include <iostream>
#include <termios.h>
void toggle_canonical(bool on) {
struct termios terminal_settings;
// Get the existing terminal settings
tcgetattr(0 /* stdin */, &terminal_settings);
if(on) {
// Disable canonical mode
terminal_settings.c_lflag &= ~ICANON;
// Read at least one character.
terminal_settings.c_cc[VMIN] = 1;
} else {
// Enable canonical mode
terminal_settings.c_lflag |= ICANON;
}
tcsetattr(0 /* stdin */, TCSANOW, &terminal_settings);
return;
}
int main(const int argc, const char* argv[]) {
// The read timeout, which can be 0 if you don't want to block at all.
struct timeval to = {5 /* seconds */, 0 /* miliseconds */};
fd_set read_fds;
int ret = 0;
// Turn canonical mode on
toggle_canonical(true);
FD_ZERO(&read_fds);
FD_SET(0, &read_fds);
// The first parameter to select() is the highest file
// descriptor in the set + 1, so in this case because
// STDIN == 0, we pass 1. This is actually completely
// ignored on several platforms, including Windows.
if((ret = select(1, &read_fds /* read set */, NULL /* write set */, NULL /* error set */, &to /* timeout */)) == 0) {
std::cout << "You didn't type anything in time." << std::endl;
} else if (ret == 1) {
std::cout << "Yay, you typed something in time!" << std::endl;
} else if (ret == -1) {
std::cout << "Oh no, an error occured!" << std::endl;
}
// Turn canonical mode off
toggle_canonical(false);
return 0;
}