簡體   English   中英

多線程文件讀取對每個線程產生相同的結果

[英]Multi-threaded file reading produces the same result for each thread

基本上,標題中是我遇到的問題,我試圖創建一個多線程應用程序來讀取和總結文件的內容,這可以在一個線程中正常工作。 但是,當引入更多時,它們的輸出相同。 我該如何解決?

編碼

void *sumThread(void *);
pthread_mutex_t keepOut = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t keepOutSum = PTHREAD_MUTEX_INITIALIZER;
int counter = 0, line_count = 0;
char* loc;
double total = 0;

void split(const string& s, char c, vector<string>& v)
{
    string::size_type i = 0;
    string::size_type j = s.find(c);

    while (j != string::npos)
    {
        v.push_back(s.substr(i, j - i));
        i = ++j;
        j = s.find(c, j);

        if (j == string::npos)
            v.push_back(s.substr(i, s.length()));
    }
}

int main(int argc, char* argv[])
{

    if (argc < 2)
    {

        cerr << "Usage: " << argv[0] << " filename" << endl;
        return 1;
    }

    string line;
    loc = argv[1];
    ifstream myfile(argv[1]);
    myfile.unsetf(ios_base::skipws);

    line_count = std::count(std::istream_iterator<char>(myfile),
                            std::istream_iterator<char>(),
                            '\n');

    myfile.clear();
    myfile.seekg(-1, ios::end);
    char lastChar;
    myfile.get(lastChar);
    if (lastChar != '\r' && lastChar != '\n')
        line_count++;

    myfile.setf(ios_base::skipws);
    myfile.clear();
    myfile.seekg(0, ios::beg);

    pthread_t thread_id[NTHREADS];

    for (int i = 0; i < NTHREADS; ++i)
    {
        pthread_create(&thread_id[i], NULL, sumThread, NULL);
    }

    for (int i = 0; i < NTHREADS; ++i)
    {
        pthread_join(thread_id[i], NULL);
    }

    cout << setprecision(2) << fixed << total << endl;
    return 0;
}

void *sumThread(void *)
{

    pthread_mutex_lock(&keepOut);
    int threadNo = counter;
    counter++;
    pthread_mutex_unlock(&keepOut);

    ifstream myfile(loc);
    double runningTotal = 0;
    string line;

    if (myfile.is_open())
    {
        for (int i = threadNo; i < line_count; i += NTHREADS)
        {
            vector < string > parts;

            getline(myfile, line);
            // ... and process out the 4th element in the CSV.
            split(line, ',', parts);

            if (parts.size() != 3)
            {
                cerr << "Unable to process line " << i
                        << ", line is malformed. " << parts.size()
                        << " parts found." << endl;
                continue;
            }

            // Add this value to the account running total.
            runningTotal += atof(parts[2].c_str());
        }
        myfile.close();
    }
    else
    {
        cerr << "Unable to open file";
    }

    pthread_mutex_lock(&keepOutSum);

    cout << threadNo << ":  " << runningTotal << endl;
    total += runningTotal;
    pthread_mutex_unlock(&keepOutSum);
    pthread_exit (NULL);
}

樣品輸出

 2:  -46772.4
 0:  -46772.4
 1:  -46772.4
 3:  -46772.4
 -187089.72

每個線程應該讀取並總結文件中的數字,然后在完成后將它們加在一起。 但是,即使threadNo變量與輸出中指示的明顯不同,所有線程似乎都返回相同的數字。

您的問題在這里:

for (int i = threadNo; i < line_count; i += NTHREADS) {
    vector<string> parts;

    getline(myfile, line);

getline()不知道i的值,因此它仍從文件中讀取相鄰的行,而不會跳過任何行。 因此,所有線程都讀取文件的相同的前幾行。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM