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

  • Home
  • SEARCH
  • 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 8248479
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T23:12:07+00:00 2026-06-07T23:12:07+00:00

I’ve got a small program that does a large amount of processing. The progress

  • 0

I’ve got a small program that does a large amount of processing. The progress of which you can get a print of by hitting the enter key.

The way I’ve implemented this is by having the processing done in the main thread whilst I have a pthread constantly looping on getchar() to wait for the enter key.

The problem is when I have finished with the processing. When this happens the main thread finishes, but still waits for enter to be pressed because getchar() is blocking.

How do I “cancel” getchar()?

  • 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-06-07T23:12:10+00:00Added an answer on June 7, 2026 at 11:12 pm

    The most portable solution I can think of is:

    • Use pipe() to construct two FDs, one a reader and the other a writer. Give the reader to your read() loop; give the writer to whoever needs to terminate the reader.
    • Use select() from the read thread to wait for readability of both stdin and the reader pipe.
    • If stdin becomes readable, read a character, process it, and then restart the loop.
    • If the reader pipe becomes readable, close it and terminate the loop.

    Now, all you should have to do is close the other end of the pipe and this will wake up the reader thread out of its select() and it should then terminate.

    The traditional approach involves using signals, however this pipe-based solution allows you to check for input on stdin as well as check if you should terminate using the same polling mechanism.


    Note that mixing getchar() and select() will not work, since getchar() will effectively use fread() under the hood, and the buffering performed by fread() can cause select() to block even though there is data available. Use read() instead. Here is an example program I used to test this approach.

    #include <stdio.h>
    #include <pthread.h>
    #include <unistd.h>
    #include <sys/select.h>
    
    void * entry_point(void * p) {
        int readpipe = *(int *)p;
        fd_set rfds;
    
        char c;
    
        for (;;) {
            FD_ZERO(&rfds);
            FD_SET(STDIN_FILENO, &rfds);
            FD_SET(readpipe, &rfds);
    
            while (select(readpipe + 1, &rfds, NULL, NULL, NULL) == 0);
    
            if (FD_ISSET(readpipe, &rfds)) {
                close(readpipe);
                break;
            }
    
            if (FD_ISSET(STDIN_FILENO, &rfds)) {
                if (read(STDIN_FILENO, &c, sizeof(c)) > 0) {
                    printf("Read: %d\n", c);
                }
            }
        }
    
        printf("Thread terminating\n");
    
        pthread_exit(NULL);
    }
    
    int main() {
        pthread_t thread;
        int r;
        int pipes[2];
    
        pipe(pipes);
    
        if (r = pthread_create(&thread, NULL, entry_point, &pipes[0])) {
            printf("Error: %d\n", r);
            return 1;
        }
    
        sleep(5);
    
        printf("Closing pipe and joining thread.\n");
    
        close(pipes[1]);
        pthread_join(thread, NULL);
    
        pthread_exit(NULL);
    }
    

    Example run:

    $ time ./test
    1
    Read: 49
    Read: 10
    2
    Read: 50
    Read: 10
    3
    Read: 51
    Read: 10
    4
    Read: 52
    Read: 10
    5
    Read: 53
    Read: 10
    Closing pipe and joining thread.
    Thread terminating
    
    real    0m5.004s
    user    0m0.004s
    sys     0m0.000s
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
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
I am trying to understand how to use SyndicationItem to display feed which is
I have a jquery bug and I've been looking for hours now, I can't
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into

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.