C++ C++;将引用变量传递给g++-4.7.4
当线程启动时,我需要启动一个传递复杂参数(std::thread)作为参数的线程。我使用的是'std::ref。这段代码适用于更新的环境(在Ubuntu上运行的g++-4.8.2) 现在,我必须在一个旧的编译器(g++4.7.4)中编译相同的代码,我会遇到错误 代码以及错误如下所示: ReaderThread.hppC++ C++;将引用变量传递给g++-4.7.4,c++,multithreading,c++11,gcc,g++,C++,Multithreading,C++11,Gcc,G++,当线程启动时,我需要启动一个传递复杂参数(std::thread)作为参数的线程。我使用的是'std::ref。这段代码适用于更新的环境(在Ubuntu上运行的g++-4.8.2) 现在,我必须在一个旧的编译器(g++4.7.4)中编译相同的代码,我会遇到错误 代码以及错误如下所示: ReaderThread.hpp class ReaderThread { void start(Reader reader, SyncController &syncController); }
class ReaderThread {
void start(Reader reader, SyncController &syncController);
}
void ReaderThread::start(Reader reader, SyncController &syncController)
{
Do something...
}
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...
}
ReaderThread.cpp
class ReaderThread {
void start(Reader reader, SyncController &syncController);
}
void ReaderThread::start(Reader reader, SyncController &syncController)
{
Do something...
}
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...
}
main.cpp
class ReaderThread {
void start(Reader reader, SyncController &syncController);
}
void ReaderThread::start(Reader reader, SyncController &syncController)
{
Do something...
}
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...
}
intmain()
{
…做事。。。
/*
*创建并启动读卡器线程。创建的对象必须处于活动状态
*在整个线程生命周期中。
*std::ref用于作为引用传递
*/
myReader=ReaderFactory(参数);
std::shared_ptr ptr(新的读线程);
std::thread th(&ReaderThread::start、ptr、myReader、std::ref(syncController));
…做其他事情。。。
}
错误:
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>)'
在/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\u 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,
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:在“struct std::_Result_of_impl”的实例化中:
/usr/gcc-4.7.4/lib/gcc/i586 pc linux gnu/4.7.4/../../../../../../../../include/c++/4.7.4/type_traits:1857:12:从'class std::result_of'中需要
/usr/gcc-4.7.4/lib/gcc/i586 pc linux gnu/4.7.4/../../../../../../../../../include/c++/4.7.4/functional:1563:61:必须来自“struct std:_Bind\u simple”
/usr/gcc-4.7.4/lib/gcc/i586 pc linux gnu/4.7.4/../../../../../../../../include/c++/4.7.4/thread:133:9:std::thread::thread(_Callable&&,_Args&&…[with_Callable=void(readerthreader::*)(Reader,SyncController&);_Args={std::shared(std::shared(ptr&,),Reader&,)std::reference)wrapper
/aeirtu/aeirtu/main.cpp:155:96:此处需要
/usr/gcc-4.7.4/lib/gcc/i586 pc linux gnu/4.7.4/../../../../../../../include/c++/4.7.4/type_traits:1834:9:错误:调用(std:_Mem_fn)(std::共享_ptr,Reader,std::reference_wrapper)不匹配
我无法确定此错误是由在旧编译器上使用std::ref
引起的,还是由其他原因引起的
帮助您找到4.7.4支持的修复程序并编译我的代码。gcc 4.7似乎无法处理作为对象提供的共享\u ptr(或唯一\u ptr)。不过,它可以很好地使用自然指针,所以一个可能的解决方案是用以下内容替换线程创建(当然,如果合适的话): 现在,如果这是不可行的,并且需要真正的分配指针,那么以下是您想法的替代方案:
std::thread th(
[ptr, &syncController, myReader] () {
ptr->start(myReader, syncController);
}
);
}
在本例中,SyncController是通过引用传递的,其他所有内容都是通过值传递的,与您的文章中的方式相同。虽然GCC4.8.x几乎完全支持c++11,但GCC4.7.x缺少一些功能。检查这里:确实如此,但我需要修复编译代码。。。感谢帮助…使用指针而不是引用。被自然指针替换,现在它工作正常。。。无论如何,我也会尝试第二个建议。。。谢谢。@Mendez,自然的指针,当然,是最前卫的。这里唯一需要注意的是,如果您需要管理对象的生命周期,自然指针可能不起作用。