Consider the following uncomplicated code:
#include <thread>
#include <utility>
#include <vector>
#include <atomic>
#include <queue>
#include <iostream>
#include <functional>
using namespace std;
template<class It, class Fun>
void parallel_for(size_t num_threads, It first, It end, const Fun& fun) {
std::queue<std::thread> ts;
for (It it = first; it != end; it) {
if (std::distance(first, it) % num_threads == 0) {
fun(*it);
} else {
if (ts.size() == num_threads-1) {
ts.front().join();
ts.pop();
}
ts.push(std::thread(fun, std::ref(*it)));
}
}
while (not ts.empty()) {
ts.front().join();
ts.pop();
}
}
int main() {
std::atomic_int counter = 1;
auto lam = [&counter](auto& vl) {
vl = std::pair(counter , -1);
};
// The following usage of std::ref works okay:
pair<int, int> x;
auto blam = bind(lam, ref(x));
blam();
// Nevertheless, the next line fails:
// lam(ref(x));
// As well as the next two ones:
// vector<pair<int, int>> v = {{4, 2}};
// parallel_for(thread::hardware_concurrency(), begin(v), end(v), lam);
return 0;
}
GCC's error on the last two lines, in particular, is
In file included from ./src/csc_cpp/passing_lambdas.cpp:1:
/usr/include/c /10/thread: In instantiation of ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = const main()::<lambda(auto:1&)>&; _Args = {std::reference_wrapper<std::pair<int, int> >}; <template-parameter-1-3> = void]’:
./src/csc_cpp/passing_lambdas.cpp:22:26: required from ‘void parallel_for(size_t, It, It, const Fun&) [with It = __gnu_cxx::__normal_iterator<std::pair<int, int>*, std::vector<std::pair<int, int> > >; Fun = main()::<lambda(auto:1&)>; size_t = long unsigned int]’
./src/csc_cpp/passing_lambdas.cpp:47:71: required from here
/usr/include/c /10/thread:136:44: error: static assertion failed: std::thread arguments must be invocable after conversion to rvalues
136 | typename decay<_Args>::type...>::value,
| ^~~~~
I am sure this is a trivial matter, but I am anyway struggling to understand this. I think I have been following the available examples on std::thread::thread()
's intended use quite closely, but this does not compile. What am I doing wrong?
CodePudding user response:
About
lam(ref(x));
x
is an lvalue
while the ref(x)
is a temporary reference_wrapper
. You can not grab a temporary with an lvalue reference in your lam
through auto&
.
for that line, you can simply use
lam(x);
CodePudding user response:
First, let me clarify, because I'm not sure if it's obvious: the trick behind std::ref
is that it returns an object of type std::reference_wrapper<T>
, so you can use the result as object, but the object is implicitly convertible to T&
, so it can be substituted where T&
is needed.
lam(ref(x));
fails because you use auto
in lam
. Compiler doesn't know that you want vl
to be std::pair<int, int>&
, it deduces from what it gets. std::ref
returns a temporary of std::reference_wrapper<std::pair<int, int>>
, which cannot be bound to non-const
reference. Use explicit type in lambda and it compiles:
auto lam = [&counter](std::pair<int, int>& vl) {
vl = std::pair(counter , -1);
};
lam(std::ref(x));
Alternatively, you can explicitly convert to std::pair<int, int>&
using get()
or static_cast
auto lam = [&counter](auto& vl) {
vl = std::pair(counter , -1);
};
lam(std::ref(x).get());
lam(static_cast<std::pair<int, int>&>(std::ref(x)));
The second part with parallel_for
has exactly the same issue, you pass rvalue of std::reference_wrapper
to lam
.