multithreading - C++ Passing a reference variable to the thread in g++-4.7.4 -
i need start thread passing complex parameters (std::thread<>) parameter when thread starts. i´m using `std::ref. code works fine updated environments (g++-4.8.2 running on ubuntu).
now have compile same code in old compiler (g++4.7.4) , i´m getting errors.
the code shown below, error:
readerthread.hpp
class readerthread { void start(reader reader, synccontroller &synccontroller); }
readerthread.cpp
void readerthread::start(reader reader, synccontroller &synccontroller) { something... }
main.cpp
int main() { ...do stuff... /* * create , start reader thread. created object must live * during whole thread life. * std::ref used pass reference */ myreader = readerfactory(params); std::shared_ptr<readerthread> ptr(new readerthread); std::thread th(&readerthread::start, ptr, myreader, std::ref(synccontroller)); ...do other stuff... }
error:
in file included /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/move.h:57:0, /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/stl_pair.h:61, /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/stl_algobase.h:65, /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/char_traits.h:41, /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/ios:41, /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/ostream:40, /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/iostream:40, ./main.cpp:11: /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits: in instantiation of 'struct std::_result_of_impl<false, false, std::_mem_fn<void (readerthread::*)(reader, synccontroller&)>, std::shared_ptr<readerthread>, reader, std::reference_wrapper<synccontroller> >': /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits:1857:12: required 'class std::result_of<std::_mem_fn<void (readerthread::*)(reader, synccontroller&)>(std::shared_ptr<readerthread>, reader, std::reference_wrapper<synccontroller>)>' /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/functional:1563:61: required 'struct std::_bind_simple<std::_mem_fn<void (readerthread::*)(reader, synccontroller&)>(std::shared_ptr<aeirtuthread::readerthread>, reader, std::reference_wrapper<synccontroller>)>' /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/thread:133:9: required 'std::thread::thread(_callable&&, _args&& ...) [with _callable = void (readerthread::*)(reader, synccontroller&); _args = {std::shared_ptr<readerthread>&, reader&, std::reference_wrapper<synccontroller>}]' ./aeirtu/aeirtu/main.cpp:155:96: required here /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/type_traits:1834:9: error: no match call '(std::_mem_fn<void (readerthread::*)(reader, synccontroller&)>) (std::shared_ptr<readerthread>, reader, std::reference_wrapper<synccontroller>)'
i can´t find out if error being caused use of std::ref
on older compilers or different.
help appreciated fo find fix supported 4.7.4 , compile code.
gcc 4.7 doesn't seem able handle shared_ptr (or unique_ptr matter) provided object. works fine natural pointer, though - 1 possible solution replace thread creation following (if appropriate, of course):
std::thread th(&readerthread::start, &mythread, myreader, std::ref(synccontroller));
now, if not feasible , true allocated pointers needed, following replacement idea:
std::thread th( [ptr, &synccontroller, myreader] () { ptr->start(myreader, synccontroller); } ); }
in example, syncontroller passed reference, else value, same way in post.
Comments
Post a Comment