Vector 保留打印输出

Vector Keeps over printing output

#include <iostream>
#include <stdlib.h>
#include <pthread.h>
#include <fstream>
#include <sstream>
#include <mutex>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <vector>

using namespace std;

#define NUM_THREADS 2
pthread_mutex_t mutexChild;
pthread_cond_t condChild;

vector<int> vect;

void openfile(ifstream&, string);

void* childthread(void *args)
{
    
    ifstream inFile;
    openfile(inFile, "Fruits.txt");
    
    string line;
    pthread_mutex_lock(&mutexChild);
    int countt = 0;
    int lines = 0;
    int total = 0;
    
    while (!inFile.eof())
    {
        
        getline(inFile, line);
        
        countt = 0;
        
        for (int i = 0; i < line.size(); i++)
            if ((line[i] >= 'A' && line[i] <= 'Z')
                    || (line[i] >= 'a' && line[i] <= 'z'))
            {
                
                countt++;
            }
        
        lines++;
        vect.push_back(countt);
    }
    
    pthread_mutex_unlock(&mutexChild);
    
    pthread_exit(NULL);
}

void* parentthread(void *args)
{
    
    ifstream inFile;
    openfile(inFile, "Fruits.txt");
    string line;
    
    pthread_mutex_lock(&mutexChild);
    
    int lines = 0;
    int total = 0;
    
    int countt = 0;
    while (!inFile.eof())
    {
        
        getline(inFile, line);
        
        for (int i = 0; i < vect.size(); i++)
            cout << vect[i] << endl;
    }
    
    pthread_exit(NULL);
}

int main(int argc, char *argv[])
{
    
    pthread_t threads;
    int thrd1;
    //int i;
    int thrd2;
    
    pthread_mutex_init(&mutexChild, NULL);
    
    thrd1 = pthread_create(&threads, NULL, childthread, NULL);
    
    if (thrd1)
    {
        cout << "Error:unable to create thread," << thrd1 << endl;
        exit(-1);
    }
    
    pthread_t threads2;
    
    thrd2 = pthread_create(&threads2, NULL, parentthread, NULL);
    
    if (thrd2)
    {
        cout << "Error:unable to create thread," << thrd2 << endl;
        exit(-1);
    }
    
// pthread_join(threads,NULL);
// pthread_join(threads2, NULL);
//threads.join();
///threads2.join();
    
    pthread_mutex_destroy(&mutexChild);
    pthread_cond_destroy(&condChild);
    
    pthread_exit(NULL);
}

void openfile(ifstream &inFile, string fname)
{
    
    inFile.open(fname);
    if (inFile.is_open())
    {
        
        // cout << "Successfully opened File" << endl;
    }
}

当我只需要打印一次时,我的子线程一直打印输出 8 次。我的 txt 文件中有 8 行,所以我猜这就是它打印输出 8 次的原因。任何人都可以告诉我为什么这样做以及我如何改变它。我也知道为什么要执行此任务更容易,但我需要使用 posix 线程交换数据。 它的印刷是这样的 1个 2个 3个 4个 5个 6个 7 8个 1个 2个 3个 4个 5个 6个 7 8个 8次 当我只需要 1个 2个 3个 4个 5个 6个 7 8

    while (!inFile.eof())
    {
        
        getline(inFile, line);
        
        for (int i = 0; i < vect.size(); i++)
            cout << vect[i] << endl;
    }

为文件中的每一行打印一次在子线程中构建的 vector 的内容(有点。参见 Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?)。这不是你想要的。似乎没有必要在这里阅读文件。子线程已经读取了文件并完成了所有 vector 汇编工作。

解决眼前问题,重复打印数据,你需要做的就是

    for (int i = 0; i < vect.size(); i++)
        cout << vect[i] << endl;