Reputation: 11730
I have a std::iostream
object (for example fstream
), and I want to use it for asynchronous operations with boost::asio
. Is that possible? I know that asio doesn't support file operations, but sometimes it is useful to handle file IO asynchronously. I can use platform-specific native file descriptors and then use them with asio, but I think that using standard C++ streams would be more elegant in C++, and also more portable.
Upvotes: 1
Views: 875
Reputation: 51931
While Boost.Asio does not support file operations, it does provide the toolset for an application to perform file operations in an asynchronous manner. A common approach to accomplishing this is to create a thread pool with Boost.Asio. An application would post the file operation into the thread pool, returning instantly. The thread pool would then execute the operation synchronously, and invoke or post the completion handler when finished.
There are a few points to consider:
io_service
that is provided to the pool when the file operation was posted.Finally, libuv is a C libraries that provides synchronous and asynchrnous file operations. It may be able to serve as a worthwhile underlying implementation or reference material.
Upvotes: 2