Hi,
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