How to stop program for 3 sec in c++ while its running

Viewed 316

I have no idea of how to stop a program in middle can anyone help me i want my program to stop for a defined sec like 3sec or 6 sec and if you press any key it start running instantly.

3 Answers

As already stated in the comments section, there is no way to accomplish this in ISO C++. Therefore, you will have to revert to platform-specific functions.

On Microsoft Windows, you can use the function WaitForSingleObject to wait for console input and specify a timeout at the same time. That function will return as soon as there is new input or the timeout has expired. You can check the return value of the function in order to determine which one of these possibilities occurred.

On Linux, you can use select, poll or epoll to accomplish the same thing by waiting on the STDIN_FILENO file descriptor. However, that file descriptor will, by default, only provide new data for reading after the user presses ENTER. Therefore, you may want to disable canonical mode. An alternative may be to use ncurses, but I doubt that this library will be able to provide a file descriptor that is usable in select, poll or epoll. However, I am unfamiliar with ncurses.

EDIT: See the comments section for a very simple solution which uses ncurses.

Does this help you?

#include <iostream>
#include <unistd.h>
using namespace std;

int main()
{
    // Sleeping Mechanism
    for(auto i = 1; i <= 2; ++i)
    {
        cout << "Code pauses for 1 second\n";
        sleep(1); // Specify the number of seconds you want this block to sleep for
    }

    // Further code is executed once a particular key is pressed
    char ch;
    cin >> ch;
    while (ch != 'e')
    {
        cout << "Pressed " << ch << "\n";
        cin >> ch;
    }

    // Some code to be executed after pressing e only
    cout << "e pressed\n";
    return 0;
}

The second part where you wait for pressing a particular key is a workaround only and is not really a very good solution to what you are looking for.

You couldn't stop the entire process flow within the process. But there are some workarounds to stop the flow for a specific time:

Stop For A Specific Time

Using std:thread::sleep* Functions:

You can stop the entire running thread for/until a specific time using:

Stop For A Event

Using std::condition_variable:

If you want to sleep the entire running thread waiting for an event, you could use std::condition_variable:

Using The IO Operations:

Another option is to use some fake IO operations like a call to std::cin.get() (before calling you must ensure that std::cin buffer is empty)


Example

Here is a interruption with IO operation plus a time-out:

auto constexpr time_limit_s = 2;
input_value_t value = {' ', false};

auto const start_time = std::chrono::steady_clock::now();
std::thread read_input_thread(read_input_function, std::ref(value));

while (!value.read_flag && elapsed_time_from(start_time) < time_limit_s) {
        std::this_thread::yield();
}

if (!value.read_flag) {
        std::cout <<  "Timeout" << std::endl;
} else {
        std::cout <<  "Your value: " << value.value << std::endl;
}

Just for copy/paste and test: Read Input With Timeout

Related