2016-04-04 7 views
2

シンプルな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; 
} 
+5

'if(count = 5)'は常に 'true'(つまり、 '5') – BeyelerStudios

+1

linuxで開発している場合は、より良い/より多くのコンパイラ警告のためにコンパイラとしてclang ++を試してください。 AFAIK clangはif条件での割り当てについて警告します。 – Markus

+1

@Markus '-Wall'が設定されていれば' g ++ 'もそうだと思うだろう – OMGtechy

答えて

3

あなたの根本的な問題はcount = 5が割り当てであるため、常に真であるということです。 count == 5を使用する予定でした。


それはあなたのベクトルが大きなコピーとなり、特にとして、それは非常に無駄が多く、そしてあなたは、この2つの方法をやっていることは注目に値します:

  1. vectorは値でwriteDataに渡され、コピーに変更を参照することにより:
void writeData(std::vector<std::vector<std::wstring>>& in, const string& symbol)
  • 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; 
    } 
    
  • +1

    神様、私はばかなんだよ...それは疲れた気がしているだけで、 しかし、スタックトレースなしでデバッグエラーが発生しました。:/ – geostocker

    +1

    @NathanOliver解決策とともにいくつかの改善アドバイスをしたかったのですが、答えの途中で呼び出されました。私は編集でそれを修正しました。 –

    +0

    は非常に助けになりました。ただし、ソリューションにそのまま追加するとコンパイルエラーが発生します。サイズ(partVec)を見てください。 – geostocker

    関連する問題