mercredi 25 mars 2015

Is std::ref really necessary when passing references to threads?

I'm reading through C++ Concurrency in Action and in Chapter 2 I'm led to believe that even if a function prototype, e.g:



void MagicFunc(Data& myData);


is intended to be used like the following:



Data dataExample;
thread t(MagicFunc,dataExample);


I should really do this



Data dataExample
thread t(MagicFunc,std::ref(dataExample));


or otherwise the changes I expect to have happened to "dataExample" won't have take place. Specifically it states something like:



Although MagicFunc expects the second parameter to be passed by reference, the std::thread constructor t doesn’t know that; it’s oblivious to the types of the arguments expected by the function and blindly copies the supplied values. When it calls Magicfunc, it will end up passing a reference to the internal copy of data and not a reference to data itself. Consequently, when the thread finishes, these updates will be discarded as the internal copies of the supplied arguments are destroyed, and process_widget_data will be passed an unchanged Data myData rather than a correctly updated version.



However, testing this out with the following program



#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;
}


I've tried encasing vecVals in a std::ref and without, both times it executes without problem. Is the std::ref then really necessary and the information provided erroneous?


Thanks


Aucun commentaire:

Enregistrer un commentaire