std::this_thread::sleep_for() freezing the program

Jiaar

I am making a loading type function so what I wanted to do was to halt my program for a few seconds and then resume the execution inside a loop to make it look like a loading process. Looking up on web I found that I can use std::this_thread::sleep_for() to achieve this (I am doing this on linux). The problem I am facing is that I am unable to make it work with \r or any other way to overwrite the last outputted percentage as the program freezes as soon as I do it, however it works perfectly with \n and it's all confusing to understand why it'd work with a newline sequence but not with \r.

I am posting the sample code below, can someone tell me what am I doing wrong?

#include<iostream>
#include<chrono>
#include<thread>

int main()
{
    for (int i = 0; i<= 100; i++)
    {
        std::cout << "\r" << i;
        std::this_thread::sleep_for(std::chrono::seconds(rand()%3));
    }
    return 0;
}

I am kinda new to this topic and after some digging I found out that I am messing with the threads. But still not sure why this behavior is happening.

digito_evo

This works fine on my machine (Windows 10):

#include <iostream>
#include <chrono>
#include <thread>
#include <cstdlib>
#include <ctime>


int main( )
{
    std::srand( std::time( 0 ) );

    for ( std::size_t i { }; i < 100; ++i )
    {
        std::cout << '\r' << i;
        std::this_thread::sleep_for( std::chrono::seconds( std::rand( ) % 3 ) );
    }
}

I suggest that you write '\r' instead of "\r" cause here you only want to print \r character and "\r" actually prints two characters (\r and \0).
Also, it's better to seed rand using srand before calling it.

However, it may not work as expected in some environments so you may need to flush the output sequence like below:

std::cout << '\r' << i << std::flush;

or like this:

std::cout << '\r' << i;
std::cout.flush( );

These are equivalent.

For more info about flush see here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

std::this_thread::sleep_for() vs sleep() in main()

std::condition_variable not working with std::this_thread::sleep_for()

Code generated - usleep vs std::this_thread::sleep_for

What is s in std::this_thread::sleep_for(2s)?

Can std::this_thread::sleep_for() have spurious wakeups?

std::this_thread::sleep_for sleeps for too long

Why is `std::this_thread::yield()` 10x slower than `std::this_thread::sleep_for(0s)`?

How to make a thread stop excution (eg: std::this_thread::sleep_for) for an accturate interval

std::this_thread::sleep_for sleeps shorter than expected in VS2015

Why doesn't std::this_thread::sleep_for() work in MacOS terminal?

this_thread::sleep_for sleeps early

Does std::condition_variable::wait_until have any advantage against std::this_thread::sleep_for?

C# Wait without making the thread sleep and freezing the program.

this_thread::sleep_for / SDL Rendering Skips instructions

What is the maximum value I can pass to std::thread::sleep_for() and sleep_until()?

Why does this_thread::sleep_for not reduce CPU usage of while loop

Why doesn't this_thread::sleep_for need to be linked against pthread?

Where is std::this_thread for jthread?

C++11 std::this_thread - How to cancel sleep_until ()?

C++11 std::this_thread::sleep_until() hangs when compiled with GCC 4.8.5

std::this_thread::sleep_until timing is completely off by about a factor of 2, inexplicably

Unpredictable behaviour of std::sleep_for on Windows 10

prevent sleep_for from blocking background thread

Can this_thread::sleep() be interrupted on linux?

std::thread class vs std::this_thread namespace in c++?

Thread.sleep of main thread is freezing the app, android

Why is this tkinter program freezing?

Python script freezing program

Program freezing if function is present after the freezing point