I am playing with threads but I don't know why this code doesn't work properly. It has to read two files(the names of the files come from keyboard) and then show the contents in the console. Could you tell me where is the problem?
#include<iostream>
#include<fstream>
#include<thread>
#include<string>
#include<vector>
using namespace std;
mutex mtx;
class eu
{
string filename;
string buffer;
public:
eu() {};
void citire(string filename)
{
this->filename = filename;
ifstream f(filename);
while (f.eof())
{
string ceva;
f >> ceva;
buffer = buffer + ceva;
}
f.close();
}
/*void operator()() it doesn't work either in this way
{
this->citire(filename);
}*/
friend ostream &operator<<(ostream& os, eu&n);
//~eu();
};
ostream& operator<<(ostream& os, eu&n)
{
os << n.buffer;
return os;
}
//threads.push_back(std::thread(&C::increase_member,std::ref(bar),1000));
int main()
{
vector<thread>instante;
string nume;
eu n;
for (int i = 0; i <2; i++)
{
cin >> nume;
instante.push_back(thread(&eu::citire, n, nume));
}
for (auto& th : instante) th.join();
cout << n;
}