User input without pausing code (c++ console appli

2019-06-11 17:20发布

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 >> string; pauses the code AFAIK.

Would I need to use mulithreading, or is there a better way? (I don't even know if multithreading would work.)

I've recently started learning c++, I am a beginner to say the least, so please explain thoroughly and include any library I might need, thank you.

3条回答
Evening l夕情丶
2楼-- · 2019-06-11 17:35

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.

查看更多
贪生不怕死
3楼-- · 2019-06-11 17:37

use kbhit().

while(1)
{
    if(kbhit())
    {
        // do what ever you want.
    }
}
查看更多
Bombasti
4楼-- · 2019-06-11 17:44

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.

查看更多
登录 后发表回答