tags:

views:

70

answers:

3

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?

A: 

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.

ezpz
simply speaking, my idea is witless. perhaps, it is
theorist
+1  A: 

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..
    }
}
Hans Passant
i'm new to the concept of callback functions. do you mean that i can include in the main program a callback function that, in my case, updates the progress bar and checks whether the process must be aborted, and call the library function with the callback function as one of the arguments? while processing the list the function calls the passed callback function and then acts as instructed, right?
theorist
and still my second question: will this be more efficient than iterating through the list in the main program and let the function only process a single item?
theorist
That's correct. I don't see an obvious efficiency angle, just cleaner code.
Hans Passant
all right, will it be faster?
theorist
A: 

Perhaps you should use boost::signal. This is at least as good as using callback functions.

Alexandre C.
i'd prefer standard C++ tools
theorist