Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

std::thread - read from file line by line

I would like to read in parallel line by line from output file. Every thread read one line then work with data. In the mean time next thread have to read the next line.

std::ifstream infile("test.txt");
std::mutex mtx;

void read(int id_thread){
   while(infile.good()){
     mtx.lock();
     std::string sLine;
     getline(infile, sLine);
     std::cout << "Read by thread: " << id_thread;
     std::cout << sLine << std::endl;
     mtx.unlock();
   }
}

void main(){
  std::vector<std::thread> threads;
  for(int i = 0; i < num; i++){
     threads.push_back(std::thread(parallelFun, i));
  }

  for(auto& thread : threads){
      thread.join();
  }
  return 0;
}

When i run this code i get this: First thread read all lines. How can i make it happen that every thread read one line?

enter image description here

EDIT

As mentioned in the comments all i needed to do was bigger test file. Thanks guys!

like image 758
dropky Avatar asked Jan 09 '23 11:01

dropky


1 Answers

I would change the loop into

while(infile.good()){
     mtx.lock();
     std::string sLine;
     getline(infile, sLine);
     mtx.unlock();
     std::cout << "Read by thread: " << id_thread;
     std::cout << sLine << std::endl;
   }

Your std::cout stuff is the busy part of your test loop which you want to exchange for real code later. This gives the other thread time to kick in. Furthermore make your test file large. It is not uncommon that thread initialization takes some time in which the first thread eats all the data.

like image 186
Oncaphillis Avatar answered Jan 12 '23 01:01

Oncaphillis