gpt4 book ai didi

c++ - 看似空的 vector 图

转载 作者:搜寻专家 更新时间:2023-10-31 01:36:00 31 4
gpt4 key购买 nike

我在一个简单的 C++ 程序中添加了一些轻微的多线程,但在此过程中遇到了一些问题。

最新的这些问题是 historical::assignthreads 出于某种原因从函数 historical::writeData 接收到一个空 vector 。

查看下面的代码,您会看到 writeData 遍历一个 vector 并将数据放在占位符中,然后再将其发送到 assignthreads(5 次迭代后)——这意味着从 writeData 发送到 assignthreads 的 vector 不应该是空。

但是在 assignthreads 中你会看到有两个 cout:s,一个在循环之前,一个在循环之后。两者都在循环甚至没有开始的情况下写入 cout。

有人知道为什么会这样吗?

void historical::writeData(std::vector<std::vector<std::wstring>> in, const string& symbol) {
std::cout << "Sending data to database connector" << std::endl;
std::vector<std::vector<std::wstring>> temp;
std::vector<std::vector<std::wstring>>::iterator it;
int count = 0;
for (it = in.begin(); it != in.end(); it++) {
if (count = 5) {
cout << "I'm in count 5" << endl;
assignthreads(temp, symbol);
temp.clear();
count = 0;
}
else {
cout << "I'm in count 0" << endl;
temp.push_back(*it);
count++;
}

}
if (!temp.empty()) {
cout << "I'm in empty" << endl;
assignthreads(temp, symbol);
}
else cout << "I'm empty!!" << endl;
}
void historical::assignthreads(std::vector<std::vector<std::wstring>>& partVec, const string& symbol) {
int i = 0;
cout << "I'm in assign" << endl;
vector<thread> threads(size(partVec));
std::vector<std::vector<std::wstring>>::iterator it;
for (it = partVec.begin();
it != partVec.end();
it++) {
cout << "I'm in the loop" << endl;
std::shared_ptr<database_con> sh_ptr(new database_con);
threads.at(i) = std::thread(&database_con::start, sh_ptr, *it, symbol);
i++;
}
cout << "I've finished" << endl;
for (auto& th : threads) th.join();

}

void historical::writer(string* pInput) {
ofstream mf("test.csv");
if (mf.is_open()) {
mf << *pInput;
mf.close();
}
else cout << "Unable to open file" << endl;
}

最佳答案

这里你的根本问题是 count = 5是一项任务,因此始终为真。您打算使用 count == 5 .


值得注意的是,特别是当你的 vector 变大时,复制它是非常浪费的,你正在这样做 2 种方式:

  1. vector传递到 writeData按值,更改为按引用复制:void writeData(std::vector<std::vector<std::wstring>>& in, const string& symbol)
  2. temp最终将复制 in 的每个元素,请改用迭代器,因此您的代码必须更改为:
#define SIZE 5

void assignthreads(std::vector<std::vector<std::wstring>>::iterator start, std::vector<std::vector<std::wstring>>::iterator finish, const string& symbol) {
cout << "I'm in assign" << endl;

vector<thread> threads(distance(start, finish));

for(auto i = 0; start != finish; ++i, ++start) {
cout << "I'm in the loop" << endl;
std::shared_ptr<database_con> sh_ptr(new database_con);
threads.at(i) = std::thread(&database_con::start, sh_ptr, *start, symbol);
}
cout << "I've finished" << endl;

for (auto& th : threads) th.join();
}

void writeData(std::vector<std::vector<std::wstring>>& in, const string& symbol) {
std::cout << "Sending data to database connector" << std::endl;

auto count = 0;

while(count < in.size() - SIZE) {
auto start = next(in.begin(), count);

count += SIZE;

auto finish = next(in.begin(), count);

assignthreads(start, finish, symbol);
}

assignthreads(next(in.begin(), count), in.end(), symbol);

cout << "I'm empty!!" << endl;
}

关于c++ - 看似空的 vector 图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36403742/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com