2016-04-28 3 views
0

私は複数のテキストファイル(1つの投票を表す候補の1人の名前を含む各ファイルの各行)から投票を集計する簡単なプログラムに取り組んでいます。一貫性のないタリーベクトルとpthreadsを使用

私はそれがほとんど一方、残りの問題で、作業があります。その候補がvotes.txtファイルに投票まったく同じ数を持っていない限り、各候補者のための投票の合計数は、与えられた候補者のために不正確です。注文は違いはありません。例えば

votes1.txtがターゲットディレクトリにあるファイルのみで、候補者のそれぞれの1票がある場合:

votes1.txt: 

Clinton 
Cruz 
Kasich 
Sanders 
Trump 

をプログラムが生成し、最終的な集計が正しくそれぞれのすべてのための1票を持つことになります私はプログラムを実行する。

同様に、ターゲットディレクトリにvotes1.txtvotes2.txtがあり、それぞれが各候補に対して任意の順番で1票の投票権を持っている場合、最終集計にはそれぞれ2票が正しく表示されます。

しかし、候補者の投票数が異なるテキストファイルが始まると、私は最終集計で不正確で不一致な合計を得るようになります。

EG次のようvotes1.txtvotes2.txtの内容がある場合:

votes1.txt: 

Clinton 
Cruz 
Kasich 
Sanders 
Trump 

votes2.txt: 

Clinton 
Cruz 
Kasich 
Clinton 
Sanders 
Trump 

最終集計は、クリントンのために正しくないであろう(この場合、それはまだ2の代わりに3あろう)。しかし、ファイルが両方ともクリントンのために2票を持っていれば、最終的な票数はクリントンにとって合計4票、残りはそれぞれ2票でもう一度正しいだろう。最終的な合計は、ファイルが大きくなるほど大きく離れるほど大きくなります(votes.txt)。

これは非常に奇妙で具体的なエラーですが、私のコードを徹底的に調べれば、原因を特定することはできません。

がここに関連するコードです:

int main(int argc, char *argv[]) 
{ 
    vector<string> voteTallyFiles; 
    vector<voteTally> intermVoteTallies; 
    voteTally finalVoteTally; 
    string args; 
    voteTally returnValue; 
    void *returnValueP; 
    void *argp; 
    int index = 0; 

    //Grab vote tally files from Target Directory 
    getFileNames(voteTallyFiles); 

    vector<pthread_t> threads(voteTallyFiles.size()); 

    //Count the votes using pthreads and a thread function 
    for (vector<pthread_t>::iterator iter = threads.begin(); iter != threads.end(); ++iter, index++) 
    { 
     args = voteTallyFiles[index]; 
     argp = &args; 
     pthread_create(&*iter, NULL, &countVotes, argp); 
    } 

    // Wait for the threads 
    for (vector<pthread_t>::iterator iter = threads.begin(); iter != threads.end(); ++iter) 
    { 
     pthread_join(*iter, &returnValueP); 
     returnValue = *((voteTally*)returnValueP); 
     intermVoteTallies.push_back(returnValue); 
    } 

    // Aggregate sub-tallies 
    for (unsigned int i = 0; i < intermVoteTallies.size(); i++) 
    { 
     finalVoteTally.pres1 += intermVoteTallies[i].pres1; 
     finalVoteTally.pres2 += intermVoteTallies[i].pres2; 
     finalVoteTally.pres3 += intermVoteTallies[i].pres3; 
     finalVoteTally.pres4 += intermVoteTallies[i].pres4; 
     finalVoteTally.pres5 += intermVoteTallies[i].pres5; 
    } 

    return 0; 
} 

void* countVotes(void *argp) 
{ 
    string* actualArgs = (string*)argp; 
    string fileName = *actualArgs; 

    string line; 
    ifstream inFile; 
    voteTally *subTally = new voteTally; 

    inFile.open(fileName.c_str()); 

    while (getline(inFile, line)) 
    { 
     if (line == PRES1) 
     { 
      subTally->pres1++; 
     } 
     else if (line == PRES2) 
     { 
      subTally->pres2++; 
     } 
     else if (line == PRES3) 
     { 
      subTally->pres3++; 
     } 
     else if (line == PRES4) 
     { 
      subTally->pres4++; 
     } 
     else if (line == PRES5) 
     { 
      subTally->pres5++; 
     } 
    } 
    return subTally; 
} 

答えて

3

はここで一つの問題だ:あなたがスレッドにargpを渡すが、彼らはすべてまったく同じローカル変数を指している上のラインで

args = voteTallyFiles[index]; 
argp = &args; 
pthread_create(&*iter, NULL, &countVotes, argp); 

argsは、すべてのスレッドが同じデータを使用する可能性があることを意味します。

代わりにアドレス演算子を使用してvoteTallyFiles[index]へのポインタを渡します

pthread_create(&*iter, NULL, &countVotes, &voteTallyFiles[index]); 
+0

あなたが鮮やかです。それを完全に修正しました。これは初めてスレッドを使ってプログラミングするので、何を探すべきかの感覚をまだ開発しています。本当にありがとう! – user3776749

関連する問題