vendredi 4 février 2022

pass async a function off a pointer of arrays? C++

I have started to want to use async io, via an array of std::future<> objects

std::future<std::vector<std::string>> results_array[32];

but when I try to pass my function, must I pass multiple instances of the function struct containing my callable ? because I am trying to give each Future.. it's own thread with it's own Connection object

    std::future<std::vector<std::string>> results_array[32];
    DataFetcher* fetcher_array[32];

    for (int i=0 ; i < 32; i++) {
       fetcher_array[i] = new DataFetcher(*this->tma_conn_pool);
    }

    for (int i=0 ; i < this->tma_conn_pool->size()-1; i++) {
      std::cout << "[hndlr-" << this->handler_num << "] request_matching_symbols() batch #" << i << " ";
      std::vector<std::string> subvector = {queries_list.begin() + (batch_size*i), queries_list.begin() + (batch_size*(i+1))};
      results_array[i] = std::async(std::launch::async, *(fetcher_array[i]).process_data, fetcher_array[i], subvector);
    }

    for (int i=0 ; i < this->tma_conn_pool->size()-1; i++) {
      results_array[i].wait();
    }

The compilation error

[ 16%] Building CXX object CMakeFiles/ibrokers_server.dir/src/server.cpp.o
/home/server.cpp: In member function ‘virtual void iHandler::request_matching_symbols(std::vector<t::MA>&, const std::vector<std::__cxx11::basic_string<char> >&)’:
/home/server.cpp:177:77: error: request for member ‘process_data’ in ‘fetcher_array[i]’, which is of pointer type ‘DataFetcher*’ (maybe you meant to use ‘->’ ?)
  177 |       results_array[i] = std::async(std::launch::async, *(fetcher_array[i]).process_data, fetcher_array[i], subvector);
      |                                                                             ^~~~~~~~~~~~
make[2]: *** [CMakeFiles/server.dir/build.make:63: CMakeFiles/server.dir/src/server.cpp.o] Error 1
make[1]: *** [CMakeFiles/Makefile2:76: CMakeFiles/server.dir/all] Error 2

If I move the de-reference.. I'm met with a different error

 error: invalid use of non-static member function ‘std::vector<std::__cxx11::basic_string<char> > DataFetcher::process_data(std::vector<std::__cxx11::basic_string<char> >&)’
  177 |       results_array[i] = std::async(std::launch::async, (*fetcher_array[i]).process_data, fetcher_array[i], subvector);
      |                                                         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
/home/server.cpp:31:28: note: declared here
   31 |   std::vector<std::string> process_data(std::vector<std::string> & sub_batch_vector){
      |                            ^~~~~~~~~~~~

For reference, here's my DataFetcher Struct, declared at the top of this .cpp

struct DataFetcher {

  DataFetcher(cpool::ConnectionPool& tma_conn_pool): tma_conn_pool(&tma_conn_pool){
    std::cout << "DataFetcher() this->tma_conn_pool == "<< this->tma_conn_pool << std::endl;
  }

  std::vector<std::string> process_data(std::vector<std::string> & sub_batch_vector){

    std::for_each(sub_batch_vector.begin(), sub_batch_vector.end(),[](std::string query_str){
      std::cout << query_str << ";";
      std::this_thread::sleep_for (std::chrono::seconds(1));
    });

    cpool::ConnectionPool::ConnectionProxy proxy_conn = this->tma_conn_pool->get_connection();

    proxy_conn->is_healthy();
    //will do more later, first just testing things out
    return sub_batch_vector;
  }

  cpool::ConnectionPool * const tws_conn_pool;
};

Aucun commentaire:

Enregistrer un commentaire