Say I have this function which is to be called by another function. This function goes on and on.
How can I terminate this function from the calling function.
I am actually writing a chess code which the calculation may take endlessly. I would like to be able to stop the calculation when needed.
How can I terminate this function from the calling function.
The simple answer is you can't. The calling function isn't running anymore. The function running is the one you called.
The better answer is that what you're looking for are threads. Start a new thread, with the function to be called, and then end that thread if you need to.
I just wonder how chess programs are coded. Move generator functions could be set to run indefinitely but should respond to keyboard inputs when asked to stop. Also, the calling function could be a loop and thus the called function only runs inside the calling function.
There must be a way, just like when you are running a ping indefinitely but could be stopped by <ctrl> + <c>.
And yes, threading answers it as two functions can virtually run at the same time. But how do I implement threading.
The better answer is that what you're looking for are threads. Start a new thread, with the function to be called, and then end that thread if you need to.
There must be a way, just like when you are running a ping indefinitely but could be stopped by <ctrl> + <c>.
What you are thinking about ping programs is not exactly correct. Unless signal handlers are written, programs do not respond to user's entry of CTRL+C (SIGINT). When you hit CTRL+C it is the shell that responds to this. The shell will make a system call to send SIGINT signal to the currently running process in the current session (in foreground).
Even if you want some keyboard input to interrupt your "move generator function", you need something that will run in a loop to capture keyboard events that is separate from the function you want to terminate.
There are, however, a poor man's interrupts which can work on a single threaded program. The following pseudo code shows this idea:
bool isWorking = true;
while (isWorking) {
if the file "./interrupt.txt" exists {
// clean up code
isWorking = false; // or you can break out instead.
}
// continue working here.
// halt work for a bit.
}
This presents you another issue: you will need to find a way to create the file "interrupt.txt". Even with this model, you will still also need to figure out how to temporarily halt the actual work to check for the "interrupt". This is a very bad example, and it is very rare that someone is doing their interrupt this way. In other words, learn about "threads" if you have no experience about them. But be warned, use of threads also comes with a cost -and that will be on a separate topic.
And yes, threading answers it as two functions can virtually run at the same time. But how do I implement threading.
You don't. You use APIs to start and run threads. Look up POSIX threads. If that's not available on your system, try Boost threads.
Threading was not implemented in older chess programs . I am planning to write a chess program that is completely compatible with UCI. The UCI (chess protocol) requires that the chess program should be stoppable by the "stop" command. This is regardless of whether it is actively calculating.
Does this UCI in some way forbid threads? Threads sounds like the perfect way to do this. One thread calculating, one thread waiting for you to type "stop" and killing the calculating thread on demand.
Another solution, albeit not the best way to go, is to use the built in API of your operating system. It's not pretty, it's not portable, and it's not standard, but it might be able to accomplish what you want. Windows has a few options when it comes to reading keyboard presses. I believe there are some buried functions in other OS's as well, but again, it's not pretty and I don't suggest them. C++ also isn't designed to even recognize a keyboard, the OS handles all of that, so this isn't exactly the easiest thing to do in C++.
As Moschops said, threading is by far your best bet, but if you're set on not using them, OS specific functions/APIs are about your only option without using a third-party library designed around gaming. I wish you the best of luck however.
Thank you all. I guess I would really have to use multi-threading not only for this purpose but also to be able to calculate possible move lines simultaneously.
Moschops (5718)>>the uci does not forbid threading. I just wonder how those chess engines are coded. It is only in recent years that it is implemented in newer programs.