1

I've done something like this this:

#include <thread>

int main(int argc, char *argv[])
{
  //...
  std::thread sad_thread(MakeMeFeelSad(), params);
  sad_thread.join();
  //...
  std::thread happy_thread(MakeMeFeelHappy(), params);
  happy_thread.join();
  //...
}

Functions MakeMeFeelSad and MakeMeFeelHappy runs for about 2 mins each, I need to make in cmd something like this: I'm getting Sad/Happy..... (while functions are running)- And dots always appear until 5, then they disappear, and appear again. It's like progress bar with the dots. How can I make this ?

ETA: I mean do I need another one thread with callback ?

Romz
  • 1,437
  • 7
  • 36
  • 63

2 Answers2

3

You may use something like:

std::future<void> fut = std::async(std::launch::async, MakeMeFeelSad, params);

std::cout << "I'm getting Sad";
std::chrono::milliseconds span (100);
while (fut.wait_for(span) == std::future_status::timeout)
    std::cout << '.';
Jarod42
  • 203,559
  • 14
  • 181
  • 302
1

You can create another thread, indeed, but it is also possible to use a condition_variable object, and loop on the wait_until method. You will be able to display your dots in this loop.

See Timeout for thread.join()

Community
  • 1
  • 1
AntiClimacus
  • 1,380
  • 7
  • 22