4

I would like to write files asynchronously. I have a class with a function that takes in a vector, and the file name, and sends it to the file. This function can be called a few thousands of time, from outside the class.

The reason why I want to perform write async is... caller can just request the write, then not have to worry about it or wait for the write to be complete.

I am not using sockets, tcp...

I am looking into boost:: asio, trying to find examples - all I can find are examples that use networking: http://liveworkspace.org/code/3R3RUd%240

Serialize and send a data structure using Boost?

boost::asio async_read guarantee all bytes are read

And more.

Can someone please suggest an example for file i/o ?

Is what I am asking making sense ?

Community
  • 1
  • 1
Thalia
  • 13,637
  • 22
  • 96
  • 190

1 Answers1

7

I think what you want to do is perfectly fine, assuming that your IO-operation is sufficently complicated, in order to justfy the overhead of the async implementaion. My simple suggestion would look like this (copied from an old question):

#include <thread>
#include <functional>
#include <boost/asio.hpp>
#include <boost/bind.hpp>

void io_operation(int parameter) {
   //Your stuff
}

int main ( int argc, char* argv[] ) {
    boost::asio::io_service io_service;
    boost::asio::io_service::work work(io_service);

    //Set up a thread pool taking asynchronically care of your orders
    std::vector<std::thread> threadPool;

    for(size_t t = 0; t < std::thread::hardware_concurrency(); t++){
        threadPool.push_back(std::thread(boost::bind(&boost::asio::io_service::run, &io_service)));
    }

    //Post an IO-operation
    io_service.post(std::bind(io_operation, 123));

    //Join all threads at the end
    io_service.stop();
    for(std::thread& t : threadPool) {
        t.join();
    }
}

This assumes, that you can use C++11 threads. Also you should be aware, that this does not ensure only one thread is writing to a specific file. Therefore you may need to use strands to ensure that calls for a specific file are not handeled in parallel.

Community
  • 1
  • 1
Haatschii
  • 9,021
  • 10
  • 58
  • 95
  • Thank you. I am using VS2010... If I understand correctly, the io_operation will be the same whether writing sync or async ? And only the caller decides how to use it ? Also, if multiple threads can write to same file... I have to use some sort of locks ? But if each thread is calling the function with a different file path - is it still possible for multiple threads to write to same file ? – Thalia May 21 '13 at 01:10
  • @Thalia: Yes, the operation is the same you would use without async operation. If each thread is calling the function with a different file path you won't get any errors I assume. To ensure that you have to use some kind of lock. The Strands I linked in my post are the usual way to do this in boost asio (Have a look at the reference and the examples). However you can also use `std::mutex` in the `io_operation` function if you prefer. – Haatschii May 21 '13 at 11:16
  • @polkovnikov.ph: Admittedly there were some minor issues with my code snipped, however the idea should have been clear. – Haatschii Feb 12 '15 at 23:28