-1
#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

user4581301
  • 33,082
  • 7
  • 33
  • 54
m123332
  • 21
  • 3
  • 1
    Recommendation: change `while (!inFile.eof()) { getline(inFile, line);` to `while (getline(inFile, line)) {`. [Rationale](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons). – user4581301 Feb 16 '22 at 18:18
  • 1
    Does this answer your question? [Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?](https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-i-e-while-stream-eof-cons); – François Andrieux Feb 16 '22 at 18:19
  • 1
    Side note: instead of platform specific `pthread.h` library, you should consider the C++11 standard `` header – prapin Feb 16 '22 at 20:36

1 Answers1

1
    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;
user4581301
  • 33,082
  • 7
  • 33
  • 54
  • when I try to take it out the while loop it won't print at all. – m123332 Feb 16 '22 at 18:44
  • Make absolutely certain that parent thread isn't grabbing the mutex and completing before the child thread gets a chance to start nd populate the `vector`. I'd use a condition variable for this job rather than just a mutex. – user4581301 Feb 16 '22 at 19:11