Three threads read lines from a file, process lines in parallel and then write lines to a new file as the same order as the lines are read out. The new file should be as the same as input file. Input file is text file.
/build:
//g++ xxx.cpp -lboost_thread-mt
#include <boost/thread.hpp>
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <fstream>
using namespace std;
ifstream fp;
int lineId = 0;
ofstream fp2;
boost::mutex lineReadMutex;
boost::mutex lineSaveMutex;
boost::condition_variable lineSaveVariable;
//Next saving line
int saveLineNum = 1;
int handleLine(const string& line) {
//Simulate some heavy work
int sum = 0;
for (int loop = 0; loop < 30000; ++loop) {
for (int i = 0; i < line.size(); ++i) {
sum += (long)line[i];
}
}
return 1;
}
void saveLine(const string& line, int lineNum) {
boost::mutex::scoped_lock lock(lineSaveMutex);
// It’s my turn
while (saveLineNum != lineNum) {
lineSaveVariable.wait(lock);
}
fp2 << line << endl;
cout << “save line ” << lineNum << “\n”;
++saveLineNum;
lock.unlock();
lineSaveVariable.notify_all();
}
bool getLine(string& line, int& lineNum) {
boost::mutex::scoped_lock lock(lineReadMutex);
if (getline(fp, line)) {
lineNum = ++lineId;
return true;
}
return false;
}
void task() {
string line;
int lineNum;
while(getLine(line, lineNum)) {
int tmp = handleLine(line);
saveLine(line, lineNum);
}
}
int main(int argc, char** argv) {
if (argc < 3) return -1;
// input file name
fp.open(argv[1]);
if (!fp.is_open()) return -1;
// output file name
fp2.open(argv[2]);
if (!fp2.good()) return -1;
boost::thread thr_1(task);
boost::thread thr_2(task);
boost::thread thr_3(task);
thr_1.join();
thr_2.join();
thr_3.join();
return 0;
}