我需要在线程启动时启动一个传递复杂参数(std :: thread<>)作为参数的线程。我在使用`std :: ref。此代码适用于更新的环境(在Ubuntu上运行的g ++ - 4.8.2)。
现在我必须在旧的编译器(g ++ 4.7.4)中编译相同的代码并且我得到错误。
代码如下所示,以及错误:
ReaderThread.hpp
class ReaderThread {
void start(Reader reader, SyncController &syncController);
}
ReaderThread.cpp
void ReaderThread::start(Reader reader, SyncController &syncController)
{
Do something...
}
的main.cpp
int main()
{
...do stuff...
/*
* Create and start the reader thread. The created object must live
* during the whole thread life.
* std::ref is used to pass as 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 from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/move.h:57:0,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/stl_pair.h:61,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/stl_algobase.h:65,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/bits/char_traits.h:41,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/ios:41,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/ostream:40,
from /usr/gcc-4.7.4/lib/gcc/i586-pc-linux-gnu/4.7.4/../../../../include/c++/4.7.4/iostream:40,
from ./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 from '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 from '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 from '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 from 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 for call to '(std::_Mem_fn<void (ReaderThread::*)(Reader, SyncController&)>) (std::shared_ptr<ReaderThread>, Reader, std::reference_wrapper<SyncController>)'
我无法确定这个错误是由于在较旧的编译器上使用std::ref
还是从不同的编译器引起的。
帮助感谢找到4.7.4支持的修补程序并编译我的代码。
答案 0 :(得分:2)
gcc 4.7似乎无法处理作为对象提供的shared_ptr(或此问题的unique_ptr)。虽然它可以很好地使用自然指针 - 所以一种可能的解决方案是用以下代替线程创建(当然,如果合适的话):
std::thread th(&ReaderThread::start, &myThread, myReader, std::ref(syncController));
现在,如果这不可行并且需要真正的分配指针,则以下是您的想法的替代:
std::thread th(
[ptr, &syncController, myReader] () {
ptr->start(myReader, syncController);
}
);
}
在此示例中,synController通过引用传递,其他所有内容按值传递,与帖子中的方式相同。