C++ error: no matching member function for call to 'enqueue' futures.emplace_back(TP.enqueue(sum_plus_one, x, &M));

38 views Asked by At

Good evening, I am trying to parallelize a code that sums plus one to all the elements of a vector M, with a ThreadPool (the pool is standard and it is implemented in the book Parallel Programming Concepts Snd Practice). So I defined a function sum_plus_one(x,M) that takes as arguments x: the position of the vector, and the vector M. The code is the following:

#include <iostream>
#include <vector>
#include <thread>
#include <hpc_helpers.hpp>
#include <threadPool.hpp>



int main(int argc, char *argv[]) {

// define a Thread Pool with 8 Workers
ThreadPool TP(5);

auto sum_plus_one = [](const uint64_t x, const std::vector<uint64_t> &M) {
    return M[x]+1;
};

uint64_t N = 10;   // dimension of the vector

// allocate the vector
std::vector<uint64_t> M(N);
std::vector<std::future<uint64_t>> futures;

// init function
auto init=[&]() { 
    for(uint64_t i = 0; i< N; ++i) {  
        M[i] = 1;           
    }
};

init();


// enqueue the tasks in a linear fashion
for (uint64_t x = 0; x < N; ++x) {
       futures.emplace_back(TP.enqueue(sum_plus_one, x, &M));
}

// wait for the results
for (auto& future : futures)
    std::cout << future.get() << std::endl;


return 0;
}

But I get the following error

error: no matching member function for call to 'enqueue'
               futures.emplace_back(TP.enqueue(somma, x, &M));    candidate template ignored: substitution failure [with Func = (lambda at UTWavefrontTP.cpp:20:15) &, Args = <unsigned long long &, std::vector<unsigned long long> *>]: no type named 'type' in 'std::result_of<(lambda at UTWavefrontTP.cpp:20:15) &(unsigned long long &, std::vector<unsigned long long> *)>'
        auto enqueue(Func && func, Args && ... args) -> std::future<Rtrn> {

I am approaching this type of things as a beginner and I am a bit struggling finding the solution. Seems like the pointers are all okay, and that enqueue cannot find the function that sums to one all the elements.

0

There are 0 answers