我正在阅读《 C++并发性操作》,在第2章中,我相信即使函数原型(prototype),例如:

void MagicFunc(Data& myData);

旨在按以下方式使用:
Data dataExample;
thread t(MagicFunc,dataExample);

我真的应该这样做
Data dataExample
thread t(MagicFunc,std::ref(dataExample));

否则,我期望对“dataExample”进行的更改将不会发生。具体来说,它表示如下内容:



但是,请使用以下程序对此进行测试
#include <iostream>
#include <thread>
#include <vector>
#include <chrono>
#include <assert.h>
using namespace std;
using namespace std::chrono;

const int NUM_VALS = 50000000;

#define _MULTICORE

void AddValuesToSlots(vector<int>& vecVals,vector<int>::iterator& begin,
                      int num,int startNum){
    int i = startNum;
    auto end = begin + num;
    for (auto itr = begin; itr < end; ++itr){
        *itr = i++;
    }
}

int main()
{
    vector<int> vecVals;
    vecVals.resize(NUM_VALS);

    //get number of cores and divide up the workload
    unsigned int numCores = thread::hardware_concurrency();
    unsigned int slotsPerThread = NUM_VALS / numCores;

    //for timing
    high_resolution_clock::time_point t1 = high_resolution_clock::now();


    thread* t = new thread[numCores];

    //get the iterator to the beginning
    auto begin = vecVals.begin();

#ifdef _MULTICORE
    for (int core = 0; core < numCores; ++core){
        t[core] = thread(AddValuesToSlots, vecVals, begin + core*slotsPerThread,
            slotsPerThread, core*slotsPerThread);
    }

    for (int core = 0; core < numCores; ++core){
        t[core].join();
    }
#else
    AddValuesToSlots(vecVals, begin, NUM_VALS, 0);
#endif


    delete[] t;

    //how long did it take?
    high_resolution_clock::time_point t2 = high_resolution_clock::now();
    cout << duration_cast<milliseconds>(t2-t1).count() << endl;

#ifdef _DEBUG
    //test that the values are correct
    for (int slot = 0; slot < NUM_VALS; ++slot)
        assert(vecVals[slot] == slot);
#endif

    return 0;
}

我试过将vecVals封装在std::ref中,但没有,两次执行都没有问题。那么std::ref真的必要吗,提供的信息是否错误?

谢谢

最佳答案

您不是直接更改vecVals。迭代器可以正常工作,因为可以复制迭代器,但它仍指向相同的内存地址

09-08 08:43