0

I'm trying to write a program that executes Quicksort using two threads (one for a partition and one for another). The important code looks like this:

// 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();
}

I get this error by the compiler, which I can't fully understand:

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

Compilation is g++ -g -O2 -std=c++14 thrtest.cpp -o thrtest

I looked through many posts and tried many things to solve this, but I must be getting something wrong.

1 Answers1

0

QuickSort is an overloaded function, you have to cast it:

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());
O'Neil
  • 3,790
  • 4
  • 16
  • 30