views:

335

answers:

1

I'm coding in c++, and I'm trying to load an image file asynchronously. After some research, I found some mentions about using boost::asio and boost::iostreams to do it. However, the documentation and example for boost::asio is mostly socket related, so it doesn't help me much.

Here is what I need:

  • Load a file asynchronously and upon load completion, executes a callback function.(In my case, the callback function executes a javascript function object using v8 javascript engine)
  • The callback function must be executed within the same thread as the main function. ( Because v8 is not thread safe.)
  • Need to work on linux and windows. (separate implementations are ok)

So, something like this would be really nice:

async_read("test.jpg", &the_callback_function);

The function should not block, and upon file load completion, it should run 'the_callback_function'.

Edit: as joshperry pointed out, boost::asio might not be able to dispatch back to the main thread. So, I guess I don't have to limit to only boost::asio and boost:iostreams. Any c/c++ library that can help with this requirement should be fine. Thanks!

+1  A: 

You can do what you want with a little more scaffolding, but in order for the callback to be executed on your main thread, the main thread must be waiting on something which signals that the callback is ready. Here's one way to do it. I'm assuming that your main thread already has some form of execution loop.

  • Add a thread safe notification queue, which background threads can use to notify the main thread of callbacks to be executed.
  • Modify your main execution loop to wait on that queue along with whatever other event sources it waits on (obviously, I'm assuming you have an event-driven loop in your main thread, adjust to taste if you don't :).
  • Continue to use asio async_read with a callback, only the callback won't directly execute the completion function, instead it will queue it to the main thread for execution.

It's not hard to build such a queue portably using STL and synchronization objects (such as those provided by Boost.Thread). Building it to scale to high-throughput without contention is another matter, but it doesn't sound like you need that kind of scaling. Even if other libraries will do all this for you, building something like this yourself is a great exercise for anyone doing multithreaded programming, particular if one or more of the components involved are not re-entrant.

aalpern