1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
|
#include <iostream> // for standard I/O
#include <string> // for strings
#include <iomanip> // for controlling float print precision
#include <sstream> // string to number conversion
#include <ctime>
#include <future>
#include <fstream>
#include <string>
/*
*
* error This file requires compiler and library support for the ISO C++ 2011 standard.
* This support is currently experimental, and must be enabled with the -std=c++11 or -std=gnu++11 compiler options.
*
*/
using namespace std;
stringstream processing (int x,int id) {
std::cout << "Calculating. Please, wait...\n";
stringstream cvsStream;
for(int i = 0 ; i < x ; ++i){
cvsStream <<i<<","<<i<<","<<i<<","<<i<<"\n";
cout <<id<< " / "<<i<< endl;
}
return cvsStream;
}
int main(int argc, char *argv[])
{
string filename = "OutputFile.csv";
ofstream myfile;
stringstream cvsStream;
myfile.open(filename);
// If file does not exist, Create new file
if (!myfile )
{
cout << "Cannot open file, file does not exist. Creating new file..";
myfile.open(filename, fstream::in | fstream::out | fstream::trunc);
myfile <<"\n";
}
// open csv file
cvsStream <<" AD_ID "<<","<<"Starts at "<<","<<"At_Frame"<<","<<"Ends at "<<"\n";
myfile << cvsStream.str();
cvsStream.str("");
auto outputRslt1 = std::async (processing,1000,1);
auto outputRslt2 = std::async (processing,1000,2);
auto outputRslt3 = std::async (processing,1000,3);
stringstream rsltThread1 = outputRslt1.get();
stringstream rsltThread2 = outputRslt2.get();
stringstream rsltThread3 = outputRslt3.get();
// close csv file
myfile << rsltThread1.str();
myfile << rsltThread2.str();
myfile << rsltThread3.str();
myfile.close();
return 0;
}
|