i did some research googling regarding the usageof ifstream within a thread as function argumentbut mangaed to find its c implementation in pthread. what i am looking is i want itto be imlemented in c++11 ,so am implementing c++ threads here.
the thing is i am trying to pass ifstream as argument to thread but i get the following error:- error: no type named ‘type’ in ‘class std::result_of))(std::basic_ifstream)>’ typedef typename result_of<_Callable(_Args...)>::type result_type; how do i pass the ifstream argument to the function being called on thread heres my code below
#include <cstdlib>
#include <fstream>
#include <thread>
#include <iostream>
using namespace std;
void file_func_1(std::ifstream m_file){
// ifstream m_file;
string line;
while ( getline (m_file,line) ){
cout<<"the line now in is"<<line<<"from thread"<<std::this_thread::get_id()<<endl;
std::this_thread::sleep_for(std::chrono::seconds((rand() % 10) + 1));
}
}
void file_func_2(ifstream m_file){
string line;
while ( getline (m_file,line) ){
cout<<"the line now in is"<<line<<"from thread"<<std::this_thread::get_id()<<endl;
std::this_thread::sleep_for(std::chrono::seconds((rand() % 10) + 1));
}
}
int main(int argc, char** argv) {
std::ifstream m_file;
m_file.open("File_line.txt",ios::in);
int name=8;
std::thread func_th1(file_func_1, (m_file));
// thread func_th2(file_func_2,m_file);
func_th1.join();
// func_th2.join();
return 0;
}
Aucun commentaire:
Enregistrer un commentaire