Output to cin from workflow (C ++)

My program has a main thread that accepts input from a user. Separately, it can potentially propagate (at least 1) worker threads fooling data in the background.

The user can terminate the program using the command by entering it into the console. However, when a data failure occurs, the main thread still blocks waiting for user input and, therefore, the program does not exit.

I would like to know how to write the terminate command, "q \ n", in std::cinfrom a workflow, so that the lock command input stream (also the main thread) terminates . Or will it be bad? I tried below, but the program just crashes or cannot write to 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);
    }

}

in 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 );
    }
}

-, , , , ?

+3
2

Unix, (, std:: in , , ), select()... pipe(), , ... , cin, select() , ..

Windows , , WaitForMultipleObjects() .

+3

"" , , , . .

.

, , . , -.

+2

Source: https://habr.com/ru/post/1728630/


All Articles