0

I'm sure that what I'm asking is doable in some way but I have no idea what it's called. So I can't really search for it ..

I'd like to display a text (in console) progress bar for a function that is merely a loop for, from 0 to max. I know how I can do the progress bar using "\r", but it requires to add it inside the loop..

What would be the way to send info from a running function (here the incremental number) to another .. ?

Please point me to a duplicate as I'm sure it is.

Thank you.

user2287453
  • 149
  • 1
  • 8

2 Answers2

3

In order to have two functions running independently and simultaneously, they need to be running in their own threads. Normally a program executes in a single thread. A program which has multiple threads is called a "multithreaded" program.

There are a few Right reasons to implement multithreading in your program. One of those Right reasons is to seperate the GUI code from the code which does actual work, so that the GUI can run independantly of that other work.

Multithreaded programming is hard. Well, writing a multithreaded program in easy; especially these days with std::thread etc. But multithreaded programs are inherently much more complex than their single-threaded brethren because of the complex interactions between the threads. Multithreaded programs are difficult to write correctly, and are much harder to understand and maintain.

In order to get started in multithreaded programming, I'd refer you to:

The Definitive C++ Book Guide and List

Community
  • 1
  • 1
John Dibling
  • 99,718
  • 31
  • 186
  • 324
  • Why do the two functions have to be running concurrently? Why doesn't a simple callback to update the progress work? – leemes Jan 29 '14 at 17:48
  • @leemes: Because then the GUI will be unresponsive. – John Dibling Jan 29 '14 at 17:59
  • @leemes: Also, I took that to be what this question was really all about: how to have two functions running at the same time. That's what "send info from a running function to another" suggested to me. – John Dibling Jan 29 '14 at 18:01
  • There is no GUI if he wants to print the progress to the console. Also, sending information to another function can mean that the second function is not running simultaneously, but only "when information is being sent", i.e. it's a simple callback. OP said he doesn't know what it's called, so you shouldn't have set your focus on running two functions simultaneously, but how to print a progress within a loop without the printing logic within the loop but a separate function. And a callback solves this just fine, a lot easier compared to multi-threading. – leemes Jan 29 '14 at 18:59
  • @leemes: That's how I interpreted the question. Given that the OP accepted it, the helpfullness of my answer was apparently greater than zero. If you interpret the question another way, feel free to provide another answer. – John Dibling Jan 29 '14 at 19:08
  • I would, if he had answered my question in the comments. Since he didn't, I don't feel like helping. – leemes Jan 29 '14 at 22:34
  • @leemes, because i have no idea what is (and how to do) a "report progress" callback. You've already helped by naming it. I'll just have to read about it somewhere else. – user2287453 Jan 30 '14 at 13:47
0

Consider it as a Producer/Consumer pattern. You need to use multiple threads. Simple method is to use a global variable, and critical sectors in both threads to access it.

Elliot Chen
  • 378
  • 2
  • 9