Reputation: 169
is there a way to implement such a communication in C++?
use case:
my main program calls a function of my external library to process a list. every time the function iterates through the list it sends a ping to the caller. the latter uses the received signal to track progress of the former and sends back a boolean pong [true: okay, move on; false: the user wants to abort the process, return now].
will this be more efficient than iterating through the list in the main program and let the function only process a single item? or the whole idea is just crap?
Upvotes: 0
Views: 534
Reputation: 56976
Perhaps you should use boost::signal
. This is at least as good as using callback functions.
Upvotes: 0
Reputation: 942050
Sounds to me like you are talking about a callback function. A function pointer:
typedef bool (* Callback)(int mumble);
void processList(Callback notify) {
while (processingList) {
if (!notify(42)) break;
// etc..
}
}
Upvotes: 1
Reputation: 12047
If it is the whole list you are processing, why not just use a return value from the function call to indicate when the iteration is complete. If you want to continue, call the function again - if not, dont.
Upvotes: 0