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

前端 未结 3 1413
青春惊慌失措
青春惊慌失措 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 <iostream>
    #include <thread>
    #include <future>
    
    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.

    0 讨论(0)
  • 2021-01-17 02:34

    There are two algorithms for getting input without blocking (pausing). The first is polling, the second is by event (interrupt).

    Polling For Input

    Polling involves periodically checking for input. With a keyboard, this could be reading the keyboard for a keypress. With serial ports, it could mean checking the status of the receive register.

    Blocking or waiting for input on some systems would consist of polling forever, until an input is received.

    Input Events

    On some platforms, an event is sent when input is detected. For example, Windows OS receives an event that a key was pressed and sends the message to the task in focus. On embedded systems, the hardware could dereference a function pointer at an interrupt vector.

    Blocking for input on event based systems means sleeping until the event is received.

    Summary

    The standard C++ language does not provide a standard function for retrieving input without blocking. The implementation of the C++ input functions is platform dependent and may or may not block. For example, the platform could wait until a newline is received before returning a single character.

    Many platforms or operating systems have functionality where you can test a port for input (polling) or be notified when the input has occurred (event driven). Since you didn't specify which platform you are using, the details stop here.

    0 讨论(0)
  • 2021-01-17 02:43

    use kbhit().

    while(1)
    {
        if(kbhit())
        {
            // do what ever you want.
        }
    }
    
    0 讨论(0)
提交回复
热议问题