User input without pausing code (c++ console application)

前端 未结 3 1412
青春惊慌失措
青春惊慌失措 2021-01-17 01:47

How can I enter an input without causing the code to stop executing? I have been searching for an answer during the last 20 minutes without result.

cin >>

3条回答
  •  囚心锁ツ
    2021-01-17 02:23

    Here's an example of how you can read a token from the standard input stream using >> in parallell with another call using multithreading.

    #include 
    #include 
    #include 
    
    int main() {
        // Enable standard literals as 2s and ""s.
        using namespace std::literals;
    
        // Execute lambda asyncronously.
        auto f = std::async(std::launch::async, [] {
            auto s = ""s;
            if (std::cin >> s) return s;
        });
    
        // Continue execution in main thread.
        while(f.wait_for(2s) != std::future_status::ready) {
            std::cout << "still waiting..." << std::endl;
        }
    
        std::cout << "Input was: " << f.get() << std::endl;
    }
    

    Here the call to std::async with the launch parameter std::launch::async runs a task asynchronously on a different thread. std::async returns a handle to the task run in parallel called a future. This future can be used to check if the task is finished or not. The result of the task can also be returned from the std::future object using the member function std::future::get.

    The task I'm passing to std::async is a simple lambda expression that creates a string, reads a token from the stream into the string and returns it (the ""s is a standard literal introduced in C++14 that returns an empty std::string object).

    After calling std::async the main thread continues executing statements (in parallel with the asynchronously run task). In my example a while loop is executed that simply waits for the asynchronous task to finish. Note the use of std::future::wait_for that only blocks for 2 seconds on every iteration. std::future::wait_for returns a status that can be compared against to check if the task is finished or not.

    Finally the call to std::future::get will return the result of the asynchronously run task. If we hadn't have waited for the task to finish, the call would block until the result was ready.

    Be careful not to read from the standard input in the main thread also (in parallel) as the read call is not an atomic transaction and the data may be garbled.

提交回复
热议问题