シンプルなC++プログラムに多少のマルチスレッドを追加し、途中でいくつかの問題が発生しました。 見かけ上空のベクトル
、これらの問題の最新
は 歴史:: assignthreads何らかの理由は、歴史的な機能 :: WRITEDATAから空のベクターを受信していることです。下記のコードを見ると、writeDataがベクトルを反復して、そのデータをplaceholderに配置してからassignthreads(5回の反復後)に転送することがわかります。つまり、writeDataからassignthreadsに送信されるベクトルは空である。
しかし、assignthreadsでは、ループの前後に2つの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;
}
'if(count = 5)'は常に 'true'(つまり、 '5') – BeyelerStudios
linuxで開発している場合は、より良い/より多くのコンパイラ警告のためにコンパイラとしてclang ++を試してください。 AFAIK clangはif条件での割り当てについて警告します。 – Markus
@Markus '-Wall'が設定されていれば' g ++ 'もそうだと思うだろう – OMGtechy