C++ 将引用变量传递给 g++-4.7.4 中的线程
C++ Passing a reference variable to the thread in g++-4.7.4
我需要启动一个线程,在线程启动时将复杂参数 (std::thread<>) 作为参数传递。我正在使用 `std::ref。此代码适用于更新的环境(g++-4.8.2 运行 on Ubuntu)。
现在我必须在旧编译器 (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...
}
错误:
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 支持的修复程序并编译我的代码。
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 通过引用传递,其他所有内容均通过值传递,与 post.
中的方式相同
我需要启动一个线程,在线程启动时将复杂参数 (std::thread<>) 作为参数传递。我正在使用 `std::ref。此代码适用于更新的环境(g++-4.8.2 运行 on Ubuntu)。
现在我必须在旧编译器 (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...
}
错误:
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 支持的修复程序并编译我的代码。
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 通过引用传递,其他所有内容均通过值传递,与 post.
中的方式相同