我一直在尝试使用std::thread
实现多线程代码。当调用没有参数的类方法时,我需要编写如下内容:
std::thread(&PointerSearcher::perform_pointer_search, this);
代码可以编译并执行。
如果我想对具有两个参数的另一种方法执行相同的操作,我的IDE(CLion
)不会显示任何错误,表明一切都很好,但是GCC
会产生一个奇怪且冗长的错误消息:
std::thread(&PointerSearcher::find_deeper_memory_pointers, this, chunked_memory_pointers, current_pointer_depth_index);
输出:
[ 9%] Building CXX object CMakeFiles/PointerSearcher.dir/src/pointer_search_objects/PointerSearcher.cpp.o
/usr/bin/c++ -fopenmp -g -DIS_DEBUG_MODE -std=gnu++1z -o CMakeFiles/PointerSearcher.dir/src/pointer_search_objects/PointerSearcher.cpp.o -c /mnt/d/Cpp/PointerSearcher/src/pointer_search_objects/PointerSearcher.cpp
In file included from /mnt/d/Cpp/PointerSearcher/src/pointer_search_objects/PointerSearcher.cpp:1:0:
/usr/include/c++/7/thread: In instantiation of 'struct std::thread::_Invoker<std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >':
/usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (PointerSearcher::*)(std::vector<MemoryPointer>&, int); _Args = {PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int&}]'
/mnt/d/Cpp/PointerSearcher/src/pointer_search_objects/PointerSearcher.cpp:741:42: required from here
/usr/include/c++/7/thread:240:2: error: no matching function for call to 'std::thread::_Invoker<std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >::_M_invoke(std::thread::_Invoker<std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >::_Indices)'
operator()()
^~~~~~~~
/usr/include/c++/7/thread:231:4: note: candidate: template<long unsigned int ..._Ind> decltype (std::__invoke((_S_declval<_Ind>)()...)) std::thread::_Invoker<_Tuple>::_M_invoke(std::_Index_tuple<_Ind ...>) [with long unsigned int ..._Ind = {_Ind ...}; _Tuple = std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int>]
_M_invoke(_Index_tuple<_Ind...>)
^~~~~~~~~
/usr/include/c++/7/thread:231:4: note: template argument deduction/substitution failed:
/usr/include/c++/7/thread: In substitution of 'template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2, 3}]':
/usr/include/c++/7/thread:240:2: required from 'struct std::thread::_Invoker<std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >'
/usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (PointerSearcher::*)(std::vector<MemoryPointer>&, int); _Args = {PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int&}]'
/mnt/d/Cpp/PointerSearcher/src/pointer_search_objects/PointerSearcher.cpp:741:42: required from here
/usr/include/c++/7/thread:233:29: error: no matching function for call to '__invoke(std::__tuple_element_t<0, std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >, std::__tuple_element_t<1, std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >, std::__tuple_element_t<2, std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >, std::__tuple_element_t<3, std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >)'
-> decltype(std::__invoke(_S_declval<_Ind>()...))
~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/7/tuple:41:0,
from /usr/include/c++/7/bits/unique_ptr.h:37,
from /usr/include/c++/7/memory:80,
from /usr/include/c++/7/thread:39,
from /mnt/d/Cpp/PointerSearcher/src/pointer_search_objects/PointerSearcher.cpp:1:
/usr/include/c++/7/bits/invoke.h:89:5: note: candidate: template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...)
__invoke(_Callable&& __fn, _Args&&... __args)
^~~~~~~~
/usr/include/c++/7/bits/invoke.h:89:5: note: template argument deduction/substitution failed:
/usr/include/c++/7/bits/invoke.h: In substitution of 'template<class _Callable, class ... _Args> constexpr typename std::__invoke_result<_Functor, _ArgTypes>::type std::__invoke(_Callable&&, _Args&& ...) [with _Callable = void (PointerSearcher::*)(std::vector<MemoryPointer>&, int); _Args = {PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int}]':
/usr/include/c++/7/thread:233:29: required by substitution of 'template<long unsigned int ..._Ind> decltype (std::__invoke(_S_declval<_Ind>()...)) std::thread::_Invoker<std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >::_M_invoke<_Ind ...>(std::_Index_tuple<_Ind1 ...>) [with long unsigned int ..._Ind = {0, 1, 2, 3}]'
/usr/include/c++/7/thread:240:2: required from 'struct std::thread::_Invoker<std::tuple<void (PointerSearcher::*)(std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int> >'
/usr/include/c++/7/thread:127:22: required from 'std::thread::thread(_Callable&&, _Args&& ...) [with _Callable = void (PointerSearcher::*)(std::vector<MemoryPointer>&, int); _Args = {PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >&, int&}]'
/mnt/d/Cpp/PointerSearcher/src/pointer_search_objects/PointerSearcher.cpp:741:42: required from here
/usr/include/c++/7/bits/invoke.h:89:5: error: no type named 'type' in 'struct std::__invoke_result<void (PointerSearcher::*)(std::vector<MemoryPointer>&, int), PointerSearcher*, std::vector<MemoryPointer, std::allocator<MemoryPointer> >, int>'
CMakeFiles/PointerSearcher.dir/build.make:185: recipe for target 'CMakeFiles/PointerSearcher.dir/src/pointer_search_objects/PointerSearcher.cpp.o' failed
make[2]: *** [CMakeFiles/PointerSearcher.dir/src/pointer_search_objects/PointerSearcher.cpp.o] Error 1
make[2]: Leaving directory '/mnt/d/Cpp/PointerSearcher/cmake-build-debug'
CMakeFiles/Makefile2:70: recipe for target 'CMakeFiles/PointerSearcher.dir/all' failed
make[1]: *** [CMakeFiles/PointerSearcher.dir/all] Error 2
make[1]: Leaving directory '/mnt/d/Cpp/PointerSearcher/cmake-build-debug'
make: *** [all] Error 2
Makefile:86: recipe for target 'all' failed
我不明白这一点,因为以下方法调用可以正常编译:
find_deeper_memory_pointers(chunked_memory_pointers, current_pointer_depth_index);
方法签名为:
void PointerSearcher::find_deeper_memory_pointers(std::vector<MemoryPointer> &memory_pointers, const int pointer_depth_index)
因此参数数据类型正确。