vendredi 28 août 2015

boost:bind and io_service in two different classes

I am kind'a new to Boost.

I would like to know how to construct an io_service in one class and send it tasks from another class.

My problem is BOOST_ASIO_COMPLETION_HANDLER_CHECK complains and wont let the code compile.

He is a skeleton of my code:

type_defs.h (this has the function to be wrapped and sent to the io_service)

#include "boost/filesystem.hpp"

namespace fs = ::boost::filesystem;

typedef boost::function<void(const fs::path& path)> parse_and_aggregate_fun_t;

io_service_processors.h (this is the class that will construct the io_service and wait for job posts).

namespace asio = boost::asio;

typedef std::unique_ptr<asio::io_service::work> work_ptr;

class io_service_processors {
public:
    io_service_processors(int tc);
    virtual ~io_service_processors();

    void init();
    void post_job(parse_and_aggregate_fun_t file_job);

private:
    int thread_count;
    asio::io_service* service;
    boost::mutex mtx;
    work_ptr * work;
    boost::thread_group workers;
};

io_service_processors.cpp

namespace asio = boost::asio;   

io_service_processors::io_service_processors(int tc):thread_count(tc) {

}

io_service_processors::~io_service_processors() {
       // clean up code here removed.
}

void io_service_processors::init() {
    this->service = new asio::io_service();
    this->work = new work_ptr(new asio::io_service::work(*(this->service)));
    for(int i = 0; i < this->thread_count; ++i)
        this->workers.create_thread(boost::bind(&asio::io_service::run, this->service));
}

void io_service_processors::post_job(parse_and_aggregate_fun_t file_job) {
    this->service->post(file_job);
}

job_discoverer.cpp (this one will discover files and send their paths to to the io_service, the function parse_and_aggregate will the actual work for opening the file and processing it, process will simply post the wrapped function to the io_service, this->processor is simply a pointer to the io_service wrapper class above).

void job_discoverer::process(const fs::path path){
    std::cout << "Posting file: " << path.string() << std::endl;
    if(this->processor)
        this->processor->post_file_job(
                boost::bind(&text_file_type::parse_and_aggregate, this, path)
                );
}

void job_discoverer::parse_and_aggregate(const fs::path path) {
    std::cout << "Parsing and aggregating file: " << path.string() << std::endl;
}

Thank you!!!!!!!!!!

Aucun commentaire:

Enregistrer un commentaire