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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T10:40:42+00:00 2026-05-13T10:40:42+00:00

My program has a main thread that takes command input from a user. Separately,

  • 0

My program has a main thread that takes command input from a user. Separately, it has potentially multiplie (at least 1) worker threads churning data in the background.

The user is able to terminate the program using a command by typing into the console. However, when the data churning is done, the main thread is still blocking waiting for user input and hence the program does not terminate.

What I would like to know is how to write the terminate command, “q\n”, into the std::cin from a worker thread so that the blocking command input thread (also the main thread) will terminate. Or would this be a bad thing to do? I’ve tried the below but the program simply hangs or not able to write to the std::cin, not sure why.

static ACE_THR_FUNC_RETURN worker( void *p) {
 .....
    if (_this->m_num_threads_done == _this->m_threads.size()) {
        fputs("q\n", stdin);
    }

}

on the main thread, this is called from main:

void runEventLoop()
{
    printWelcomeMessage();

    char buffer[MAXC];
    while( !m_exitLoop )
    {
        std::cin.getline(buffer, MAXC);
        if( std::cin.eof() )
            break;
        handleCommand( buffer );
    }
}

would someone please advise on what I’m doing wrong here or otherwise suggest a better solution for what I’m trying to accomplish?

thanks

  • 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-13T10:40:42+00:00Added an answer on May 13, 2026 at 10:40 am

    On Unix, when you need a thread to wait for multiple things (for example, a character on std::in and a command from a worker thread to communicate that it is shutting down) you use select()… You could create a pipe with the pipe() system call and the worker thread could write to it when it’s exiting… The main thread that is waiting currently on cin could call select() on both to block, then react appropriately to either when it’s woken up..

    On Windows, you can probably use WaitForMultipleObjects() for the same purpose.

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

Sidebar

Ask A Question

Stats

  • Questions 377k
  • Answers 377k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer You need to add a generic type parameter to the… May 14, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer There are a couple of stretching options that can be… May 14, 2026 at 8:59 pm
  • Editorial Team
    Editorial Team added an answer STX and ETX are characters 0x02 and 0x03 respectively, so… May 14, 2026 at 8:59 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.