A
A
Alexander Tikhonov2014-11-22 21:57:52
C++ / C#
Alexander Tikhonov, 2014-11-22 21:57:52

Printing information to the console while waiting for keyboard input. Maybe?

The actual situation:
The client has connected to the server and is blocked waiting for a command to be entered from the keyboard.
The server with which the client has connected performs some actions and, based on the results of these actions, it can send information to the client that needs to be output to the console immediately, or it may not send it. Is it possible to implement this? Maybe there is even a solution through boost
So far, only one idea comes to mind, this is to make an additional thread on the client. 1 - main, blocked on input. 2 - waits for a command from the server and displays it when it is received.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Armenian Radio, 2014-11-22
@tikhonov666

This question was discussed:
How to shift user input in C (console, multithreading)?
How to organize parallel processes in C++?
I will add that this can be done without an additional thread, using overlaid (OVERLAPPED) operations, if we are talking about windows.

A
Alexander Tikhonov, 2014-11-23
@tikhonov666

Made a sketch, it seems to work as it should

#include <iostream>
#include <string>
#include <../boost/thread.hpp>
#include <../boost/thread/mutex.hpp>
#include <../boost/bind.hpp>
boost::mutex io_mutex;
void outPut(std::string & p)
{
    sleep(5);
    {
        boost::mutex::scoped_lock lock(io_mutex);
        p+="Qt/Example";
        std::cerr<<std::endl<<p<<":~$ ";
    }
}
int main()
{
    std::string temp, path = "/";
    boost::thread thr1(boost::bind(&outPut,path));
    {
        boost::mutex::scoped_lock lock(io_mutex);
        std::cerr<<"Please";
        sleep(1);
        std::cerr<<" enter your";
        sleep(1);
        std::cerr<<" command>\n";
    }
    std::cerr<<path<<":~$ ";
    std::cin>>temp;
    std::cerr<<temp<<std::endl;
    exit(1);
    return 0;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question