Подтвердить что ты не робот

Неблокирующий std:: getline, выход, если вход отсутствует

В настоящее время у меня есть программа, которая читает со стандартного ввода, изредка программа должна просто продолжать работать, если вход не выполняется, обычно это тест script нет 'enter', так сказать.

program -v1 -v2 -v3 <input >output

v1 - v3 - аргументы командной строки, соответственно

В основном программа выплевывает аргументы командной строки и их соответствующее значение программе, если не задан "enter", а затем должен выйти.

Однако на данный момент, если вы даете ему пустой тестовый файл или просто запускаете без нажатия enter после запуска его блоков на std:: getline, я использую для ввода команд.

while(std::getline(std::cin,foo)
{do stuff}

где foo - строка.

Как мне заставить его просто запустить и do stuff по крайней мере один раз, а затем выйти в случае отсутствия ввода? В случае ввода do stuff происходит один раз для каждой строки стандартного ввода.

Работает ли переключатель в цикле do-while с предварительным циклом проверки того, получил ли он какой-либо вход?

Что-то вроде

if cin empty
set flag

do
 {do stuff
 check flag}
while(getline)

или не блокирует io невозможно в С++?

Этот вопрос, кажется, повторяется снова и снова, но я не мог найти окончательного ответа или даже ответа, который был несовместимым с платформой (эта программа носит академический характер, закодирована на окнах и протестирована в Unix).

4b9b3361

Ответ 1

Использование std :: cin асинхронно может быть единственным способом заставить это работать, так как iostream не предназначен для неблокирующего поведения. Вот пример:

Async Example 1 Async Example 2 (printing a space out every 1/10th of a second while accepting CLI input at the same time)

Код комментируется, поэтому его должно быть легко понять. Это потокобезопасный класс, который позволяет асинхронно получать строки, используя std :: cin.

Очень прост в использовании для целей getline асинхронного интерфейса командной строки, с 0% загрузки процессора на моем компьютере. Он отлично работает в Windows 10 в Visual Studio 2015 c++ Режим отладки и выпуска консоли Win32. Если это не работает в вашей ОС или среде, это очень плохо.

#include <iostream>
#include <string>
#include <thread>
#include <mutex>
#include <atomic>

using namespace std;

//This code works perfectly well on Windows 10 in Visual Studio 2015 c++ Win32 Console Debug and Release mode.
//If it doesn't work in your OS or environment, that too bad; guess you'll have to fix it. :(
//You are free to use this code however you please, with one exception: no plagiarism!
//(You can include this in a much bigger project without giving any credit.)
class AsyncGetline
{
    public:
        //AsyncGetline is a class that allows for asynchronous CLI getline-style input
        //(with 0% CPU usage!), which normal iostream usage does not easily allow.
        AsyncGetline()
        {
            input = "";
            sendOverNextLine = true;
            continueGettingInput = true;

            //Start a new detached thread to call getline over and over again and retrieve new input to be processed.
            thread([&]()
            {
                //Non-synchronized string of input for the getline calls.
                string synchronousInput;
                char nextCharacter;

                //Get the asynchronous input lines.
                do
                {
                    //Start with an empty line.
                    synchronousInput = "";

                    //Process input characters one at a time asynchronously, until a new line character is reached.
                    while (continueGettingInput)
                    {
                        //See if there are any input characters available (asynchronously).
                        while (cin.peek() == EOF)
                        {
                            //Ensure that the other thread is always yielded to when necessary. Don't sleep here;
                            //only yield, in order to ensure that processing will be as responsive as possible.
                            this_thread::yield();
                        }

                        //Get the next character that is known to be available.
                        nextCharacter = cin.get();

                        //Check for new line character.
                        if (nextCharacter == '\n')
                        {
                            break;
                        }

                        //Since this character is not a new line character, add it to the synchronousInput string.
                        synchronousInput += nextCharacter;
                    }

                    //Be ready to stop retrieving input at any moment.
                    if (!continueGettingInput)
                    {
                        break;
                    }

                    //Wait until the processing thread is ready to process the next line.
                    while (continueGettingInput && !sendOverNextLine)
                    {
                        //Ensure that the other thread is always yielded to when necessary. Don't sleep here;
                        //only yield, in order to ensure that the processing will be as responsive as possible.
                        this_thread::yield();
                    }

                    //Be ready to stop retrieving input at any moment.
                    if (!continueGettingInput)
                    {
                        break;
                    }

                    //Safely send the next line of input over for usage in the processing thread.
                    inputLock.lock();
                    input = synchronousInput;
                    inputLock.unlock();

                    //Signal that although this thread will read in the next line,
                    //it will not send it over until the processing thread is ready.
                    sendOverNextLine = false;
                }
                while (continueGettingInput && input != "exit");
            }).detach();
        }

        //Stop getting asynchronous CLI input.
        ~AsyncGetline()
        {
            //Stop the getline thread.
            continueGettingInput = false;
        }

        //Get the next line of input if there is any; if not, sleep for a millisecond and return an empty string.
        string GetLine()
        {
            //See if the next line of input, if any, is ready to be processed.
            if (sendOverNextLine)
            {
                //Don't consume the CPU while waiting for input; this_thread::yield()
                //would still consume a lot of CPU, so sleep must be used.
                this_thread::sleep_for(chrono::milliseconds(1));

                return "";
            }
            else
            {
                //Retrieve the next line of input from the getline thread and store it for return.
                inputLock.lock();
                string returnInput = input;
                inputLock.unlock();

                //Also, signal to the getline thread that it can continue
                //sending over the next line of input, if available.
                sendOverNextLine = true;

                return returnInput;
            }
        }

    private:
        //Cross-thread-safe boolean to tell the getline thread to stop when AsyncGetline is deconstructed.
        atomic<bool> continueGettingInput;

        //Cross-thread-safe boolean to denote when the processing thread is ready for the next input line.
        //This exists to prevent any previous line(s) from being overwritten by new input lines without
        //using a queue by only processing further getline input when the processing thread is ready.
        atomic<bool> sendOverNextLine;

        //Mutex lock to ensure only one thread (processing vs. getline) is accessing the input string at a time.
        mutex inputLock;

        //string utilized safely by each thread due to the inputLock mutex.
        string input;
};

void main()
{
    AsyncGetline ag;
    string input;

    while (true)
    {
        //Asynchronously get the next line of input, if any. This function automagically
        //sleeps a millisecond if there is no getline input.
        input = ag.GetLine();

        //Check to see if there was any input.
        if (!input.empty())
        {
            //Print out the user input to demonstrate it being processed.
            cout << "{" << input << "}\n";

            //Check for the exit condition.
            if (input == "exit")
            {
                break;
            }
        }

        //Print out a space character every so often to demonstrate asynchronicity.
        //cout << " ";
        //this_thread::sleep_for(chrono::milliseconds(100));
    }

    cout << "\n\n";
    system("pause");
}

Ответ 2

Вы можете использовать cin.peek, чтобы проверить, есть ли что-нибудь для чтения, а затем вызывать getline, если таковой существует. Нет такой вещи, как неблокирующая getline сама по себе, хотя.

Ответ 3

Вы можете довольно легко сделать неблокирующий эквивалент std::getline, используя метод istream :: readsome(). Это читает доступные входные данные вплоть до максимального размера буфера, без блокировки.

Эта функция всегда возвращает мгновенно, но захватывает строку, если она доступна в потоке. Частичные строки хранятся в статической переменной до следующего вызова.

bool getline_async(std::istream& is, std::string& str, char delim = '\n') {

    static std::string lineSoFar;
    char inChar;
    int charsRead = 0;
    bool lineRead = false;
    str = "";

    do {
        charsRead = is.readsome(&inChar, 1);
        if (charsRead == 1) {
            // if the delimiter is read then return the string so far
            if (inChar == delim) {
                str = lineSoFar;
                lineSoFar = "";
                lineRead = true;
            } else {  // otherwise add it to the string so far
                lineSoFar.append(1, inChar);
            }
        }
    } while (charsRead != 0 && !lineRead);

    return lineRead;
}