mardi 20 avril 2021

How can I pass multiple arguments to a function in thread (C++11)?

I have read answers to similar questions and tried to pass multiple arguments to function in the thread the same way. But it didn't work out. I am getting a long compilation error.

Here's my function:

void split(istream& inStream, int nKiloBytesPerChunk, vector<string>& vecFilenames, string ext) {
//implementation...
}

Here's how I am sending arguments to this function in the thread in main():

int main() {
   // ...
   thread t1(split, ref(ifs), chunkSizeKBs, ref(vecFilenames), ext);
   t1.join();
   // ...
}

Here's what the terminal shows:

/usr/bin/ld: /tmp/cclkxzzp.o: in function `std::thread::thread<void (&)(std::istream&, int,
std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >,
 std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, 
std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >),
 std::reference_wrapper<std::basic_ifstream<char, std::char_traits<char> > >, int&, 
std::reference_wrapper<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> > > > >, std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> >&, void>(void (&)(std::istream&, int, 
std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, 
std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > 
>&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >), 
std::reference_wrapper<std::basic_ifstream<char, std::char_traits<char> > >&&, int&, 
std::reference_wrapper<std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> > > > >&&, std::__cxx11::basic_string<char, std::char_traits<char>, 
std::allocator<char> >&)':
splitnmerge.cpp:(.text._ZNSt6threadC2IRFvRSiiRSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS
8_EES8_EJSt17reference_wrapperISt14basic_ifstreamIcS6_EERiSE_ISA_ERS8_EvEEOT_DpOT0_[_ZNSt6thread
C5IRFvRSiiRSt6vectorINSt7__cxx1112basic_stringIcSt11char_traitsIcESaIcEEESaIS8_EES8_EJSt17refere
nce_wrapperISt14basic_ifstreamIcS6_EERiSE_ISA_ERS8_EvEEOT_DpOT0_]+0x61): undefined reference to 
`pthread_create'
collect2: error: ld returned 1 exit status

I really can't get where I am getting wrong. Please look into this problem. Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire