In the following program, I was expecting an (approximately) alternating pattern of red and green dots generated by the two threads to print as, or while the program was running. However, I fail to understand why the sequence of dots are printed in blocks only after 10s of seconds are up on my M1 Mac mini. I can't see why the console can't keep up with dots printing at 1Hz
.
main.cpp
#include <thread>
#include <fmt/core.h>
#include <fmt/color.h>
void work(const fmt::color &c) {
int seconds = 600;
for (int i = 0; i < seconds; ++i) {
std::this_thread::sleep_for(std::chrono::seconds(1));
fmt::print(fmt::fg(c), ".");
}
}
int main() {
auto thread0 = std::thread([&]() {
work(fmt::color::red);
});
auto thread1 = std::thread([&]() {
work(fmt::color::green);
});
thread0.join();
thread1.join();
return 0;
}
Expected output after just beyond 3
seconds:
......
Actual output after 3
seconds
Why don't the dots print in real-time as it were?