Here is my function call using the std::thread library.
threads.push_back(std::thread(&polynomial::multiply, this, std::cref(chunck[i]), std::cref(poly), std::ref(result_vector[i].poly)));
This is my function declarations:
void polynomial::multiply(const std::unordered_map<power, coeff> &vector_current, const std::unordered_map<power, coeff> &other, std::unordered_map<power, coeff> &result)
I fail to understand why this is not compiling. Could anyone help me out.
/usr/include/c /11/bits/std_thread.h: In instantiation of
‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (polynomial::*)(const std::unordered_map<long unsigned int, int>&, const std::unordered_map<long unsigned int, int>&, std::unordered_map<long unsigned int, int>&); _Args = {const polynomial*, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >}; <template-parameter-1-3> = void]’:
poly.cpp:88:142: required from here
/usr/include/c /11/bits/std_thread.h:130:72: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
130 | typename decay<_Args>::type...>::value,
| ^~~~~
/usr/include/c /11/bits/std_thread.h:130:72: note: ‘std::integral_constant<bool, false>::value’ evaluates to false
/usr/include/c /11/bits/std_thread.h: In instantiation of ‘struct std::thread::_Invoker<std::tuple<void (polynomial::*)(const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&), const polynomial*, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > > > >’:
/usr/include/c /11/bits/std_thread.h:203:13: required from ‘struct std::thread::_State_impl<std::thread::_Invoker<std::tuple<void (polynomial::*)(const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&), const polynomial*, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > > > > >’
/usr/include/c /11/bits/std_thread.h:143:29: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (polynomial::*)(const std::unordered_map<long unsigned int, int>&, const std::unordered_map<long unsigned int, int>&, std::unordered_map<long unsigned int, int>&); _Args = {const polynomial*, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >}; <template-parameter-1-3> = void]’
poly.cpp:88:142: required from here
/usr/include/c /11/bits/std_thread.h:252:11: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (polynomial::*)(const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&), const polynomial*, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > > > >::__result<std::tuple<void (polynomial::*)(const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&), const polynomial*, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > > > >’
252 | _M_invoke(_Index_tuple<_Ind...>)
| ^~~~~~~~~
/usr/include/c /11/bits/std_thread.h:256:9: error: no type named ‘type’ in ‘struct std::thread::_Invoker<std::tuple<void (polynomial::*)(const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&), const polynomial*, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > > > >::__result<std::tuple<void (polynomial::*)(const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&, std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > >&), const polynomial*, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<const std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > >, std::reference_wrapper<std::unordered_map<long unsigned int, int, std::hash<long unsigned int>, std::equal_to<long unsigned int>, std::allocator<std::pair<const long unsigned int, int> > > > > >’
256 | operator()()
CodePudding user response:
_Args = {const polynomial*, ...
Add const
to the member function declaration:
void polynomial::multiply(const std::unordered_map<power, coeff> &vector_current, const std::unordered_map<power, coeff> &other, std::unordered_map<power, coeff> &result) const
CodePudding user response:
Using std::async, std::future and lambdas you can solve your problems like this :
#include <iostream>
#include <future>
#include <vector>
class SomeClass
{
public:
void some_function(int x)
{
std::cout << x << "\n";
}
};
int main()
{
SomeClass some_class;
{
std::vector<std::future<void>> futures;
auto future1 = std::async(std::launch::async, [&] { some_class.some_function(1); });
auto future2 = std::async(std::launch::async, [&] { some_class.some_function(2); });
futures.emplace_back(std::move(future1));
futures.emplace_back(std::move(future2));
// now vector will go out of scope and synchronize with the futures in it
// (wait for threads to complete)
}
return 0;
}