[Ndn-lib] NDN-CPP: Support for async I/O

Thompson, Jeff jefft0 at remap.ucla.edu
Thu Jul 2 15:56:59 PDT 2015

Hello all,

Up until now, an NDN-CPP application must have a loop to call face.processEvents() which checks for incoming data and manages interest timeouts. The processEvents loop can consume the CPU, event when there is no incoming data. And if the application uses multiple threads, it needs to ensure that it must call expressInterest, etc. on the same thread as processEvents (to be thread safe in manipulating internal data structures).

Now we have added optional support for Boost's ASIO io_service which uses async I/O and has its own event manager which does not consume the CPU until it gets an interrupt for incoming data. The new class ThreadsafeFace extends Face and methods like expressInterest dispatch to the io_service so that they may be called from any thread in a thread-safe way.

An application with ThreadsafeFace does not need to call processEvents. Instead it is responsible for creating, starting and stopping an io_service. This is shown in the example program test-get-async-threadsafe.cpp which calls expressinterest and shows the result from the onData callback.

This creates a Boost io_service and creates a ThreadsafeFace instead of a Face:

  boost::asio::io_service ioService;

  ThreadsafeFace face(ioService, "aleph.ndn.ucla.edu");

Internally, ThreadsafeFace creates an AsyncTcpTransport which uses the io_service to communicate. Instead of a processEvents loop, the application starts the io_service:

  boost::asio::io_service::work work(ioService);


The application also stops the io_service when it has received the expected number of callbacks to onData or onTimeout:

 if (++callbackCount_ >= maxCallbackCount_)

    // This will exit the program.


(This async I/O support is added in a modular way. It would be straight-forward to implement alternatives to ThreadsafeFace and AsyncTcpTransport which use an async I/O service other than Boost's io_service.)

NDNFS has already been updated to use ThreadsafeFace and is serving the demo content for the Firefox Add-On. The following commit shows the small number of changes needed. If you want help updating your application, let me know.

Thank you,
- Jeff T
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.lists.cs.ucla.edu/pipermail/ndn-lib/attachments/20150702/d8bef817/attachment-0001.html>

More information about the Ndn-lib mailing list