Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/148.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 什么是“a”;你好,世界&引用;例如;std::ref“参考;?_C++_C++11_Std_Ref - Fatal编程技术网

C++ 什么是“a”;你好,世界&引用;例如;std::ref“参考;?

C++ 什么是“a”;你好,世界&引用;例如;std::ref“参考;?,c++,c++11,std,ref,C++,C++11,Std,Ref,有人能举一个简单的例子来演示std::ref的功能吗?我指的是一个示例,其中只有在std::ref无法解释的情况下才使用其他一些构造(如元组或数据类型模板) 我发现了两个关于std::ref和的问题。但在第一个例子中,它涉及编译器中的一个bug,在第二个例子中,std::ref的使用示例不包含std::ref,它们涉及元组和数据类型模板,这使得理解这些示例变得复杂。当函数: 按值获取模板参数 或复制/移动转发引用参数,例如或的构造函数 std::ref是一种行为类似于引用的值类型 此示例可演

有人能举一个简单的例子来演示
std::ref
的功能吗?我指的是一个示例,其中只有在
std::ref
无法解释的情况下才使用其他一些构造(如元组或数据类型模板)

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

当函数:

  • 按值获取模板参数
  • 或复制/移动转发引用参数,例如或的构造函数
std::ref
是一种行为类似于引用的值类型

此示例可演示如何使用
std::ref

#include <iostream>
#include <functional>
#include <thread>

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;

  // The same applies for std::thread!
  std::thread( increment, std::ref(i) ).join();
  std::cout << i << std::endl;
}
void PrintNumber(inti){…}
int n=4;
std::function print1=std::bind(&PrintNumber,n);
std::function print2=std::bind(&PrintNumber,std::ref(n));
n=5;
print1()//印刷品4
print2()//印刷品5

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

另一个可能需要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; }

您希望在不同线程中运行的各种函数共享一个缓冲区对象。这个例子是从这个优秀的教程()中偷来的(希望我正确地做了归因)

//生产者-消费者问题

#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;
}
#包括
#包括
#包括
#包括
#包括
使用名称空间std;
类缓冲区{
std::互斥m;
std::条件变量cv;
std::deque队列;
常量无符号长大小=1000;
公众:
void addNum(int num){
标准:唯一锁(m);

wait(lock,[this](){return queue.size())这里使用
std::ref
的原因是
thread
的构造函数要求参数类型为
std::reference\u wrapper
这不是这里的要点,但这可能存在数据争用。可能值得指出的是,
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; }
#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;
}