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!

Aaron Qian
  • 4,477
  • 2
  • 24
  • 27
  • How are you expecting to dispatch back to your main thread when the async read is complete? – joshperry Dec 31 '09 at 15:09
  • well... I'm not sure. I was thinking of coroutine might do the job. Basically coroutine + non-blocking io should work in theory. So I guess asio might not fit the bill. – Aaron Qian Dec 31 '09 at 15:14
  • Getting back to the main thread is going to be difficult. There is no good way to signal a thread that isn't waiting. What kind of work is the main thread doing on the side? Could it check every so often and see if the file read is done? – Pace Dec 31 '09 at 15:30
  • The main thread is in a loop, so checking is ok. Basically I'm trying to make a javascript game engine. So the main thread runs the main game loop which already is handling events. As aalpern answered, using a message / event queue is a good solution. – Aaron Qian Jan 01 '10 at 20:11

1 Answers1

1

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
  • 652
  • 4
  • 8