c++ - Get member data from a thread -
i reading .csv file using multiple threads. each thread reads part of .csv file, eg. thread1 reads line:214
line:359
.
csvreader reader1("c:\\file.csv", 214, 359);
during reading process, data fields stored in vector instance.
data[i].push_back(data_field);
in main function, codes below:
csvreader reader1("c:\\file.csv", 214, 359); csvreader reader2("c:\\file.csv", 360, 517); thread t1(&csvreader::read_range, reader1); thread t2(&csvreader::read_range, reader2); t1.join(); t2.join(); vector<vector<string>> temp_data = reader1.get_data(); // here have problem
ideally, reader1.get_data()
should return data between line:214
, line:359
. when temp_data
, found not changed @ all.
may know wrong? , how can fix it?
when create threads pass copies of reader1
, reader2
, copies modified not original objects. means data added copies, , original objects unchanged. pass them reference use std::ref(reader1)
, std::ref(reader2)
.
if csvreader::read_range
function takes reference code should not compile, buggy compilers (including visual c++) accept it.
(n.b. in general should have shown definition or @ least signature of csvreader::read_range
people don't have guess code does.)
Comments
Post a Comment