看似空的传染媒介

时间:2016-04-04 13:15:06

标签: c++ vector

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

最新的问题是 historical :: assignthreads 由于某种原因从函数 historical :: writeData 接收空向量。

查看下面的代码,您将看到writeData迭代一个向量并将数据放入占位符,然后将其发送到assignthreads(在5次迭代之后) - 这意味着从writeData发送到assignthreads的向量不应该是'是空的。

但是在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;
}

1 个答案:

答案 0 :(得分:3)

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

值得注意的是,特别是当你的矢量变得大而复制时,它非常浪费,你正在做两种方式:

  1. vector按值传递到writeData,更改为按引用复制:void writeData(std::vector<std::vector<std::wstring>>& in, const string& symbol)
  2. temp最终将复制in的每个元素,而不是使用迭代器,因此您的代码必须更改为:
  3. #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;
    }