[C++11 ] std::ref&&std::reference_wrapper notes

The cause is watching "CplusplusConcurrencyInAction_PracticalMultithreading" time, which talked to initialize std :: thread of time, if the parameter list thread funtion is there a reference, you need to pass std :: ref can get the results in line with expectations.

Check found std :: ref is used to generate a std :: reference_wrapper. According to cppreference  words on it

Employing words, that sometimes in some places (such as the traditional values ​​STL containers, but also such as std :: bind) defaults to copying, which we might want to use does not match the desired reference.

See in particular the following few examples:

#include <functional>
#include <iostream>
 
void f(int& n1, int& n2, const int& n3)
{
    std::cout << "In function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
    ++n1; // increments the copy of n1 stored in the function object
    ++n2; // increments the main()'s n2
    // ++n3; // compile error
}
 
int main()
{
    int n1 = 1, n2 = 2, n3 = 3;
    std::function<void()> bound_f = std::bind(f, n1, std::ref(n2), std::cref(n3));
    n1 = 10;
    n2 = 11;
    n3 = 12;
    std::cout << "Before function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
    bound_f();
    std::cout << "After function: " << n1 << ' ' << n2 << ' ' << n3 << 'n';
}

Before function: 10 11 12
In function: 1 11 12
After function: 10 12 12

We found that the value of the direct pass in parameters n1 has not changed, while using std :: ref pass in the value of the results in line with expectations.

The reason does not meet the expected value of n1 is calling std :: bind when will copy first parameter, and then when passed to the function f, n1 reference is not passed in the definition of the main function, but n1 copied to a temporary variable to get the references. In the modification of the function f n1 will only copy the value of n1 is such that its consistency, without changing n1.

Here is anstd::reference_wrapper 的例子

#include <algorithm>
#include <list>
#include <vector>
#include <iostream>
#include <numeric>
#include <random>
#include <functional>
 
int main()
{
    std::list<int> l(10);
    std::iota(l.begin(), l.end(), -4);
 
    std::vector<std::reference_wrapper<int>> v(l.begin(), l.end());
    // 不能在 list 上用 shuffle (要求随机访问),但能在 vector 上使用它
    std::shuffle(v.begin(), v.end(), std::mt19937{std::random_device{}()});
 
    std::cout << "Contents of the list: ";
    for (int n : l) std::cout << n << ' '; std::cout << 'n';
 
    std::cout << "Contents of the list, as seen through a shuffled vector: ";
    for (int i : v) std::cout << i << ' '; std::cout << 'n';
 
    std::cout << "Doubling the values in the initial list...n 大专栏  [C++11 ] std::ref&&std::reference_wrapper  notes4;;
    for (int& i : l) {
        i *= 2;
    }
 
    std::cout << "Contents of the list, as seen through a shuffled vector: ";
    for (int i : v) std::cout << i << ' '; std::cout << 'n';
}


Contents of the list: -4 -3 -2 -1 0 1 2 3 4 5 
Contents of the list, as seen through a shuffled vector: -1 2 -2 1 5 0 3 -3 -4 4 
Doubling the values in the initial list...
Contents of the list, as seen through a shuffled vector: -2 4 -4 2 10 0 6 -6 -8 8

Finally, we return to the beginning, by creating the parameters passed to std :: thread thread funtion when the default parameter is still a copy, and above std :: bind similar situation, it is necessary to use std :: ref

References:

  * 《CplusplusConcurrencyInAction_PracticalMultithreading》2.2节
  * [std::ref](https://zh.cppreference.com/w/cpp/utility/functional/ref)
  * [std::reference_wrapper](https://zh.cppreference.com/w/cpp/utility/functional/reference_wrapper)
  * [C++ Difference between std::ref(T) and T&?](https://stackoverflow.com/questions/33240993/c-difference-between-stdreft-and-t)
  * 

Sincere appreciation, a shop unless

The use of micro-channel scan two-dimensional code to complete payment


Guess you like

Origin www.cnblogs.com/liuzhongrong/p/12371257.html