c - How to programmatically stop reading from stdin? -


fgetc() , other input functions can return when there's no data on file descriptor. can simulated console applications reading stdin typing ctrl-d on keyboard (at least on unix). how programmatically? example, how return fgetc() in reader thread in following code (nb: ignore possible race condition)?

#include <pthread.h> #include <stdio.h>  void* reader() {     char read_char;     while((read_char = fgetc(stdin)) != eof) {         ;     }      pthread_exit(null); }  int main(void) {     pthread_t thread;     pthread_create(&thread, null, reader, null);      // fgetc in reader thread return      pthread_exit(null); } 

thanks!

it seems want threads stop blocking on fgetc(stdin) when event occurs handle event instead. if that's case select() on both stdin , other message pipe thread can handle input both:

fd_set descriptor_set fd_zero(&descriptor_set);  fd_set(stdin_fileno, &descriptor_set);  fd_set(pipefd, &descriptor_set);   if (select(fd_setsize, &descriptor_set, null, null, null) < 0)  {    // select() error }   if (fd_isset(stdin_fileno, &descriptor_set)) {   // read byte stdin   read(stdin_fileno, &c, 1); }  if (fd_isset(pipefd, &descriptor_set))    // special event. else 

also note 1 thread in process should reading stdin.


Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -