什么是“你好,世界!” “std :: ref”的例子?

时间:2013-03-20 17:21:18

标签: c++ c++11 std ref

有人可以提供一个演示std::ref功能的简单示例吗?我的意思是一个例子,其中一些其他结构(如元组或数据类型模板)仅在时使用,如果没有它们就无法解释std::ref

我发现了两个关于std::ref herehere的问题。但是在第一个中,它涉及编译器中的错误,在第二个中,使用std::ref的示例不包含std::ref,它们涉及元组和数据类型模板,这些模板可以理解这些示例复杂。

4 个答案:

答案 0 :(得分:81)

您应该考虑在函数中使用std::ref

  • 按值模板参数
  • 或复制/移动转发参考参数,例如std::bindstd::thread的构造函数。

std::ref是一种行为类似于引用的值类型。

此示例可以明确使用std::ref

#include <iostream>
#include <functional>

void increment( int &x )
{
  ++x;
}

int main()
{
  int i = 0;

  // Here, we bind increment to (a copy of) i...
  std::bind( increment, i ) ();
  //                        ^^ (...and invoke the resulting function object)

  // i is still 0, because the copy was incremented.
  std::cout << i << std::endl;

  // Now, we bind increment to std::ref(i)
  std::bind( increment, std::ref(i) ) ();

  // i has now been incremented.
  std::cout << i << std::endl;
}

输出:

0
1

答案 1 :(得分:15)

void PrintNumber(int i) {...}

int n = 4;
std::function<void()> print1 = std::bind(&PrintNumber, n);
std::function<void()> print2 = std::bind(&PrintNumber, std::ref(n));

n = 5;

print1(); //prints 4
print2(); //prints 5

std::ref主要用于在使用std::bind时封装引用(但当然可以使用其他用途)。

答案 2 :(得分:7)

另一个可能需要std :: ref的地方是将对象传递给线程,在这些线程中,您希望每个线程对单个对象进行操作而不是对象的副本。

int main(){
BoundedBuffer buffer(200);

std::thread c1(consumer, 0, std::ref(buffer));
std::thread c2(consumer, 1, std::ref(buffer));
std::thread c3(consumer, 2, std::ref(buffer));
std::thread p1(producer, 0, std::ref(buffer));
std::thread p2(producer, 1, std::ref(buffer));

c1.join();
c2.join();
c3.join();
p1.join();
p2.join();

return 0; }

您希望各种线程中运行的各种函数共享一个缓冲区对象。这个例子是从这个优秀的教程(C++11 Concurrency Tutorial - Part 3: Advanced locking and condition variables (Baptiste Wicht) )中偷来的(希望我能正确地归属)

答案 3 :(得分:0)

//生产者消费者问题

#include <iostream>
#include <thread>
#include <mutex>
#include <deque>
#include <condition_variable>
using namespace std;

class Buffer {

    std::mutex m;
    std::condition_variable cv;
    std::deque<int> queue;
    const unsigned long size = 1000;

    public:
    void addNum(int num) {
        std::unique_lock<std::mutex> lock(m);
        cv.wait(lock, [this]() { return queue.size() <= size; });
        queue.push_back(num);
        cout << "Pushed " << num << endl;
        lock.unlock();
        cv.notify_all();
    }
    int removeNum() {
        std::unique_lock<std::mutex> lock(m);
        cv.wait(lock, [this]() { return queue.size()>0; });
        int num = queue.back();
        queue.pop_back();
        cout << "Poped " << num << endl;
        lock.unlock();
        cv.notify_all();
        return num;
    }

};

void producer(int val, Buffer& buf) {
    for(int i=0; i<val; ++i){
        buf.addNum(i);
    }
}

void consumer(int val, Buffer& buf){
    for(int i=0; i<val; ++i){
        buf.removeNum();
    }
}

int main() {
    Buffer b;
    std::thread t1(producer, 1000, std::ref(b));
    std::thread t2(consumer, 1000, std::ref(b));

    t1.join();
    t2.join();
    return 0;
}

在传递Buffer对象作为生产者和消费者中的引用的同时,main中还使用了std :: ref。如果未使用std::ref,则此代码将无法编译。