C++: Wait x seconds for input isn’t continuing after x time

  async-await, c++, parallel-processing, wait

What I’m trying to do is have an elevator controller that waits for 2 seconds for user input then goes to the next floor and adds the data entered into a list. I can’t seem to find out why my wait_for statements aren’t working.

void ElevatorController::read_value() {
    getline(std::cin, elevatorInput);
    gotInput = true;
    cv.notify_one();
}

void ElevatorController::requestInput() {
    cout << "Current floor number: " << motor.getCurrentFloor() << 'n';
    cout << "Input floor number or call to floor with "call" followed by the floor number: ";

    thread th = thread([this] { this->read_value(); });

    mutex mtx;
    unique_lock<mutex> lck(mtx);

    while (cv.wait_for(lck, chrono::seconds(2)) == cv_status::timeout);


    if (!gotInput) {
        nextFloor();
    }
    while (!ElevatorController::Parse(elevatorInput)) {
        cout << "Invalid input, try again" << 'n'
            << "Input floor number or call to floor with "" << CALLSTRING << "" followed by the floor number: ";
        read_value();
        while (cv.wait_for(lck, chrono::seconds(2)) == cv_status::timeout);
        if (!gotInput) {
            nextFloor();
        }
    }
    nextFloor();
}

Source: Windows Questions C++

LEAVE A COMMENT