A
Andreas Leitgeb
I've got a program that searches a large search-space
for some "solution", and this search can take a lot of time.
The search is done with a recursive function. At the moment
I have an if-statement that causes some progress information
to be written out only at a certain recursion level.
What I really want is, that the program just runs quietly,
and when I press <Enter>-key (or perhaps at regular wallclock
time intervalls), it should answer me with snapshot progress
information and continue.
I believe I will have to spawn a second thread that will
just wait for input from stdin, and set some volatile boolean
variable to true, and the recursive function in the main
thread would check this variable at appropriate times, and
if it is set, then print out the info and unset it. I don't
really care that - if I press <Enter> twice faster than the
other thread sees it - I will still only see one reaction,
but apart from that, is there anything else I should look
out for?
If instead I polled stdin for available data inside the
main (and only) thread, would it necessarily make it all
slower, or is there a way to do a real quick-check for
available data on a stream, that would be at least almost
as efficient as checking the volatile variable?
PS: Btw., it's a console-app without GUI.
for some "solution", and this search can take a lot of time.
The search is done with a recursive function. At the moment
I have an if-statement that causes some progress information
to be written out only at a certain recursion level.
What I really want is, that the program just runs quietly,
and when I press <Enter>-key (or perhaps at regular wallclock
time intervalls), it should answer me with snapshot progress
information and continue.
I believe I will have to spawn a second thread that will
just wait for input from stdin, and set some volatile boolean
variable to true, and the recursive function in the main
thread would check this variable at appropriate times, and
if it is set, then print out the info and unset it. I don't
really care that - if I press <Enter> twice faster than the
other thread sees it - I will still only see one reaction,
but apart from that, is there anything else I should look
out for?
If instead I polled stdin for available data inside the
main (and only) thread, would it necessarily make it all
slower, or is there a way to do a real quick-check for
available data on a stream, that would be at least almost
as efficient as checking the volatile variable?
PS: Btw., it's a console-app without GUI.