Home > Net >  Vector Keeps over printing output
Vector Keeps over printing output

Time:02-17

#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;
    }
}

My child threads keeps printing the output 8 times when I only need it to print once. There are 8 lines in my txt file so im guessing thats why its printing the output 8 times. Can anyone tell me why it's doing and how I change it. I also know there is an easier why to do this task but I am required to exchange data using posix threads. Its printing like this 1 2 3 4 5 6 7 8 1 2 3 4 5 6 7 8 8 times when I just need 1 2 3 4 5 6 7 8

CodePudding user response:

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

prints out the contents of the vector built in the child thread once for every line in the file (sort of. See Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?). This is not what you want. There doesn't seem to be any need to read the file here. The child thread already read the file and did all the vector assembly work.

All you should need to solve the immediate problem, repeated printing of the data, is

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