Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Boost asio for writing to files

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 ?

like image 260
Thalia Avatar asked Mar 24 '23 21:03

Thalia


1 Answers

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.

like image 100
Haatschii Avatar answered Apr 05 '23 06:04

Haatschii