我试图编写一个使用两个线程执行Quicksort的程序(一个用于分区,另一个用于另一个)。重要的代码如下所示:
// Function that makes the partitions
size_t Divide(std::vector<int> &v, size_t ini, size_t fin){
// Not so important code...
}
// Function called by threads
void QuickSort(std::vector<int> &v, size_t ini, size_t fin){
// Not so important code...
}
// Initial partition
void QuickSort(std::vector<int> &v){
size_t division = Divide(v, 0, v.size());
std::thread p1(QuickSort, std::ref(v), 0, division);
std::thread p2(QuickSort, std::ref(v), division+1, v.size());
p1.join();
p2.join();
}
编译器出现此错误,我无法完全理解:
thrtest.cpp: In function ‘void QuickSort(std::vector<int>&)’:
thrtest.cpp:57:54: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, std::reference_wrapper<std::vector<int> >, int, size_t&)’
std::thread p1(QuickSort, std::ref(v), 0, division);
^
In file included from thrtest.cpp:7:0:
/usr/include/c++/5/thread:133:7: note: candidate: template<class _Callable, class ... _Args> std::thread::thread(_Callable&&, _Args&& ...)
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/5/thread:133:7: note: template argument deduction/substitution failed:
thrtest.cpp:57:54: note: couldn't deduce template parameter ‘_Callable’
std::thread p1(QuickSort, std::ref(v), 0, division);
^
In file included from thrtest.cpp:7:0:
/usr/include/c++/5/thread:128:5: note: candidate: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/5/thread:128:5: note: candidate expects 1 argument, 4 provided
/usr/include/c++/5/thread:122:5: note: candidate: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/5/thread:122:5: note: candidate expects 0 arguments, 4 provided
thrtest.cpp:58:63: error: no matching function for call to ‘std::thread::thread(<unresolved overloaded function type>, std::reference_wrapper<std::vector<int> >, size_t, std::vector<int>::size_type)’
std::thread p2(QuickSort, std::ref(v), division+1, v.size());
^
In file included from thrtest.cpp:7:0:
/usr/include/c++/5/thread:133:7: note: candidate: template<class _Callable, class ... _Args> std::thread::thread(_Callable&&, _Args&& ...)
thread(_Callable&& __f, _Args&&... __args)
^
/usr/include/c++/5/thread:133:7: note: template argument deduction/substitution failed:
thrtest.cpp:58:63: note: couldn't deduce template parameter ‘_Callable’
std::thread p2(QuickSort, std::ref(v), division+1, v.size());
^
In file included from thrtest.cpp:7:0:
/usr/include/c++/5/thread:128:5: note: candidate: std::thread::thread(std::thread&&)
thread(thread&& __t) noexcept
^
/usr/include/c++/5/thread:128:5: note: candidate expects 1 argument, 4 provided
/usr/include/c++/5/thread:122:5: note: candidate: std::thread::thread()
thread() noexcept = default;
^
/usr/include/c++/5/thread:122:5: note: candidate expects 0 arguments, 4
provided
编译为g++ -g -O2 -std=c++14 thrtest.cpp -o thrtest
我浏览了很多帖子并试了很多东西来解决这个问题,但我一定是搞错了。
答案 0 :(得分:0)
QuickSort
是一个重载函数,你必须强制转换它:
auto quick_sort = static_cast<void (*)(std::vector<int> &, size_t, size_t)>(QuickSort);
std::thread p1(quick_sort, std::ref(v), 0, division);
std::thread p2(quick_sort, std::ref(v), division+1, v.size());