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 8447971
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:18:21+00:00 2026-06-10T10:18:21+00:00

I have a console application that prints on standard output. I want to implement

  • 0

I have a console application that prints on standard output. I want to implement the following behaviour:

  • Output is printed in a turns (2 seconds break after every turn)
  • User may (it’s optional for him) press a key to start printing other data
  • When user don’t click anything, output is just printed, nothing changes
  • When user clicks a predefined key (he may be able to do that even during output is being printed) some function should be called

At first I thought about doing it in other thread (one thread for printing, one for waiting for input), but I suppose it’s useless in this case, because it’s impossible to wait for an input in a thread.

So I found two libraries that might be able to do that:

  • Simple and Fast Multimedia Library (SFML): http://www.sfml-dev.org/index.php
  • ncurses: http://www.gnu.org/software/ncurses/ncurses.html

I don’t know either of them, so I am not sure which one I should learn to obtain a goal. Or maybe there is another, simpler solution?

OS: Unix

EDIT: g-makulik asked me to show, why it wasn’t possible to write using threads insisting, that it is possible and indeed, it is, but I don’t think this is how it should look like:

#include <pthread.h>
#include <iostream>
#include <string>
#include <stdio.h>

void* print_message_function(void *doPrint) {
    bool* vDoPrint = (bool*) doPrint;
    while (*vDoPrint) {
        sleep(0.5);
        std::cout << "Thread 1" << std::endl;
    }
    return NULL;
}

void* keyPressed(void* doPrint) {
    bool* vDoPrint = (bool*) doPrint;
    while (*vDoPrint) {
        *vDoPrint = (char) getchar() == 'k' ? false : true;
        std::cout << "THIS ISN'T DISPLAYED UNLESS 'k' PRESSED.";
    }
    return NULL;
}

int main(int argc, char* argv[]) {
    pthread_t thread1, thread2;
    int iret1, iret2;
    bool doPrint = true;
    iret2 = pthread_create(&thread2, NULL, print_message_function,
            (void*) &doPrint);
    iret1 = pthread_create(&thread1, NULL, keyPressed,
            (void*) &doPrint);

    pthread_join(thread1, NULL);
    pthread_join(thread2, NULL);

    std::cout << "Thread 1 returns: " << iret1 << std::endl;
    std::cout << "Thread 2 returns: " << iret2 << std::endl;
    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-06-10T10:18:23+00:00Added an answer on June 10, 2026 at 10:18 am

    I’ve used ncurses before and while the following solution may not be the prettiest, it should work.

    Ncurses allows you to put input into ‘no-delay’ mode. This makes getch(), which reads a character from input, non-blocking and it will immediately return ERR if user has not pressed anything.

    You can wait for the standard input file descriptor to see if the input file descriptor is ready for reading. That is, select() or epoll() or whatever you want. If you have to do something every 2 seconds, you can just put that time to select() or epoll(). If you do get a keypress, you can then decide whatever you want to do with it.

    This solution needs no multithreading.

    I have no knowledge of SFML so I don’t know if it would make this any easier. Ncurses is a C-library which may or may not be convenient for a C++ application. If you have man pages installed, you probably can see documentation on any ncurses function directly (for example, man getch).

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

Sidebar

Related Questions

I have one application that prints messages from Test.exe in console .My java program
I have a Windows console application that returns some text. I want to read
I have a written a Visual C++ console application (i.e. subsystem:console) that prints useful
I have a console application that uses a number of command line switches to
I have a console application that should do best effort logging to a database
I have a console application that communicates with a web service. Both of them
I have a console application that is trying to load a CustomConfigurationSection from a
I have a console application that sends a XML to a MVC application and
Scenario: I have a console application that needs to access a network share with
I have a .NET console application that needs to generate some HTML files. I

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.