Is it possible to execute "cout" by order?

Is it possible to get the raw output as shown but with specific order(from bottom to top)?

cout<<R"(programming )";sleep(10); cout<<R"(
          newbie at  )";sleep(5); cout<<R"(
            i'm      )"; sleep(1);


output:  programming------>printed third at the first line
           newbie at------->printed second at the second line
            i'm--------->printed first at the third line

Note: I don't mean reversing the output.

edit: as I guess the simple trick is to set their colour to black which will make them "disappear" and then by using nested for loop I can move through lines and change the colour of their one by one to white again with delay but i dont have an idea about how to do it practically

1 answer

  • answered 2020-11-24 20:58 sweenish

    #include <chrono>
    #include <future>
    #include <iostream>
    #include <thread>
    
    int main()
    {
        using namespace std::chrono_literals;
    
        auto print_first_line = std::async(std::launch::async, []() {
            std::this_thread::sleep_for(6s);
            std::cout << "programming\n" << std::flush;
        });
        auto print_second_line = std::async(std::launch::async, []() {
            std::this_thread::sleep_for(3s);
            std::cout << "newbie at\n" << std::flush;
        });
        auto print_third_line = std::async(std::launch::async, []() {
            std::this_thread::sleep_for(1s);
            std::cout << "i'm\n" << std::flush;
        });
    
        print_first_line.wait();
        print_second_line.wait();
        print_third_line.wait();
    
        return 0;
    }
    

    As stated in the comments, it is not possible to control std::cout in this way. But it is possible to use threads to control the timing for you. This is a quick & dirty example of using std::async from <future> to multi-thread your print statements.

    The first argument to std::async explicitly tells it to launch asynchronously as it can launch without creating a new thread. The second argument is a lambda with the sleep timer and the print statement.

    It is worth noting that you will likely need an additional compiler argument. My current testing is in the WSL (Debian), so the compile command I'm using is clang++ -Wall -Wextra -pthread main.cpp where -pthread is the argument enabling multi-threading. For your platform it may be different.

    As suggested, this scenario might be better served with a more generic function; something like delay_print(). Here's a possible implementation.

    #include <array>
    #include <chrono>
    #include <cstdint>
    #include <future>
    #include <iostream>
    #include <string>
    #include <thread>
    
    void delay_print(const std::chrono::duration<std::int64_t> &time,
                     std::string message)
    {
        std::this_thread::sleep_for(time);
        std::cout << message << std::flush;
    }
    
    int main()
    {
        using namespace std::chrono_literals;
    
        std::array<int, 4> seconds{7, 4, 2, 1};
        std::array<std::string, 4> phrases{"How\n", "Now\n", "Brown\n", "Cow\n"};
    
        auto print_line_01 =
            std::async(std::launch::async, &delay_print,
                       std::chrono::seconds(seconds[0]), phrases[0]);
        auto print_line_02 =
            std::async(std::launch::async, &delay_print,
                       std::chrono::seconds(seconds[1]), phrases[1]);
        auto print_line_03 =
            std::async(std::launch::async, &delay_print,
                       std::chrono::seconds(seconds[2]), phrases[2]);
        auto print_line_04 =
            std::async(std::launch::async, &delay_print,
                       std::chrono::seconds(seconds[3]), phrases[3]);
    
        print_line_01.wait();
        print_line_02.wait();
        print_line_03.wait();
        print_line_04.wait();
    
        return 0;
    }