How to call a class method with thread ?
My class:
using namespace std; class series{ private: string filename; vector<double> data; public: series(string _filename); int loaddata(const int col); void readdata() const; }; series::series(string _filename):filename(_filename) {} int series::loaddata(const int col) { ifstream input(filename); string line; if (!input) { cout << "File failed to open" << endl; return 0; } while(!input.eof()) { while(getline(input, line)){ vector<string> oneline; boost::split(oneline, line, boost::is_any_of("|")); data.push_back(boost::lexical_cast<double>(oneline[col])); } } return 1; } Calling it from main, only posting the relevant part. csv is a vector of filenames, basically vector of strings.
vector<series> ts; int col = 0; vector<thread> th; for (unsigned int i = 0; i != csv.size(); ++i) { ts.push_back(series(csv[i])); th.emplace_back(thread(&series::loaddata, ref(ts[i]), col)); } Gives the error I could not understand.
/usr/include/c++/4.8/functional: In instantiation of ‘struct std::_Bind_simple<std::_Mem_fn<int (series::*)(int)>(std::reference_wrapper<series>, int)>’: /usr/include/c++/4.8/thread:137:47: required from ‘std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = int (series::*)(int); _Args = {std::reference_wrapper<series>, int}]’ /home/d066537/ClionProjects/correlation/src/main.cpp:105:66: required from here /usr/include/c++/4.8/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<int (series::*)(int)>(std::reference_wrapper<series>, int)>’ typedef typename result_of<_Callable(_Args...)>::type result_type; ^ /usr/include/c++/4.8/functional:1727:9: error: no type named ‘type’ in ‘class std::result_of<std::_Mem_fn<int (series::*)(int)>(std::reference_wrapper<series>, int)>’ _M_invoke(_Index_tuple<_Indices...>) Solution please, Using Clion CMake for the program and yes threading works for free functions so I don't think It's matter of any compiler flag.
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -lpthread -std=c++11") If I remove ref wrapper from object I get this error:
terminate called after throwing an instance of 'std::system_error' what(): Enable multithreading to use std::thread: Operation not permitted Now updated to g++-5, without ref wrapper and again the error: CMakeFiles/correlation.dir/src/main.cpp.o: In function
`std::thread::thread<int (series::*)(int), series&, int>(int (series::*&&)(int), series&, int&&)': /usr/include/c++/5/thread:137: undefined reference to `pthread_create'
refwrapper for the object you want to use for the thread function.