Home > database >  Using std::thread to call std::sort directly
Using std::thread to call std::sort directly

Time:01-25

How can I make a thread execute std::sort() directly, without creating an intermediate function?

Something like:

std::thread t1(std::sort, data.begin(), data.end());

My (playground!) idea is letting each thread sort half of the vector and then merge it:

#include <vector>
#include <thread>
#include <iostream>
#include <algorithm>

// Type your code here, or load an example.
void myfunc(std::vector<int>& data) {
    std::sort(data.begin(), data.begin()   data.size() / 2);
    return;
}

int main()
{    
    std::vector<int> data = { 1, 8, 123, 10, -3, 15, 2, 7 };
    std::thread t1(myfunc, std::ref(data)); // works
    //std::thread t1(std::sort, data.begin(), data.begin()   data.size() / 2); // doesn't
    std::sort(data.begin()   data.size() / 2, data.end());
    t1.join();
    std::inplace_merge(data.begin(), data.begin()   data.size() / 2, data.end());

    for (auto x : data)
        std::cout << x << "\n";
}

Compiler error, when using the commented line instead of the one above (I used an online code editor):

error: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, std::vector<int>::iterator, __gnu_cxx::__normal_iterator<int*, std::vector<int> >)'
   16 | d t1(std::sort, data.begin(), data.begin()   data.size() / 2); // doesn't
      |                                                             ^

In file included from /usr/include/c  /11/thread:43,
                 from /tmp/bBJEjs0nrj.cpp:2:
/usr/include/c  /11/bits/std_thread.h:127:7: note: candidate: 'template<class _Callable, class ... _Args, class> std::thread::thread(_Callable&&, _Args&& ...)'
  127 |       thread(_Callable&& __f, _Args&&... __args)
      |       ^~~~~~
/usr/include/c  /11/bits/std_thread.h:127:7: note:   template argument deduction/substitution failed:
/tmp/bBJEjs0nrj.cpp:16:75: note:   couldn't deduce template parameter '_Callable'
   16 | d t1(std::sort, data.begin(), data.begin()   data.size() / 2); // doesn't
      |                                                             ^

In file included from /usr/include/c  /11/thread:43,
                 from /tmp/bBJEjs0nrj.cpp:2:
/usr/include/c  /11/bits/std_thread.h:157:5: note: candidate: 'std::thread::thread(std::thread&&)'
  157 |     thread(thread&& __t) noexcept
      |     ^~~~~~
/usr/include/c  /11/bits/std_thread.h:157:5: note:   candidate expects 1 argument, 3 provided
/usr/include/c  /11/bits/std_thread.h:121:5: note: candidate: 'std::thread::thread()'
  121 |     thread() noexcept = default;
      |     ^~~~~~
/usr/include/c  /11/bits/std_thread.h:121:5: note:   candidate expects 0 arguments, 3 provided

Desired output:

-3,1,27,8,10,15,123

CodePudding user response:

The most direct translation of the original code uses a template instantiation:

std::thread t1(std::sort<std::vector<int>::iterator>,
    data.begin(), data.end());

CodePudding user response:

std::sort() is a function template, and as such cannot be passed as-is to a function that expects a concrete functor (a type that behaves like a function). You need to either cast std::sort to the correct function pointer type, or else wrap it in some way that lets the compiler figure out which version of std::sort() you are trying to call.

The easiest way to deal with that is to use a lambda expression to wrap the call to std::sort() so that the compiler can do the template deduction for you. That can look like this:

std::thread t1([&](){ std::sort(data.begin()   data.size() / 2, data.end()); });

CodePudding user response:

You can use a lambda:

    std::thread t1([&data](){std::sort(data.begin()   data.size() / 2, data.end());});
  • Related