Linked by Hadrien Grasland on Fri 27th May 2011 11:34 UTC
General Development After having an interesting discussion with Brendan on the topic of deadlocks in threaded and asynchronous event handling systems (see the comments on this blog post), I just had something to ask to the developers on OSnews: could you live without blocking API calls? Could you work with APIs where lengthy tasks like writing to a file, sending a signal, doing network I/O, etc is done in a nonblocking fashion, with only callbacks as a mechanism to return results and notify your software when an operation is done?
Permalink for comment 474881
To read all comments associated with this story, please click here.
Callback fragmentation
by ndrw on Sat 28th May 2011 06:56 UTC
ndrw
Member since:
2009-06-30

When implementing a callback-based API be careful not to fragment the callbacks too much.

Say, user starts an operation on some large data structure and requests notification when that data structure changes. Perhaps the callback function does some expensive tasks, like updating the GUI etc. The operation itself requires a lot of atomic operation to be performed on the data structure, thus it generates a lot of notification events.

If you implemented this naively and update the user after every changes to the data structure (1), the user function would needlessly be called multiple times, consuming a lot of CPU time.

If you notified the user only at the successful completion of the whole task (2), then the user wouldn't be at all aware of the progress of the operation he started.

What you probably want to do instead, is to collect the notifications like in (2) and emit user code notifications periodically + immediately after the whole operation has finished. This probably requires some concurrency in your code but it can be done cooperatively if your main loop supports timers and idle events.

Reply Score: 3