使用std::thread直接调用std::sort

mh3*_*333 4 c++ sorting multithreading c++11

如何让线程std::sort()直接执行,而不创建中间函数?

就像是:

std::thread t1(std::sort, data.begin(), data.end());
Run Code Online (Sandbox Code Playgroud)

我的(游乐场!)想法是让每个线程对向量的一半进行排序,然后将其合并:

#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";
}
Run Code Online (Sandbox Code Playgroud)

当使用注释行而不是上面的行时,编译器错误(我使用在线代码编辑器):

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
Run Code Online (Sandbox Code Playgroud)

期望的输出:

-3,1,27,8,10,15,123
Run Code Online (Sandbox Code Playgroud)

Pet*_*ker 7

原始代码最直接的翻译是使用模板实例化:

std::thread t1(std::sort<std::vector<int>::iterator>,
    data.begin(), data.end());
Run Code Online (Sandbox Code Playgroud)

  • 我比我自己更喜欢这个。一个建议是,将 `std::sort&lt;std::vector&lt;int&gt;::iterator&gt;` 更改为 `std::sort&lt;decltype(data)::iterator&gt;` 以“面向未来”代码。 (2认同)

Nat*_*ica 5

std::sort()是一个函数模板,因此不能按原样传递给需要具体函子(行为类似于函数的类型)的函数。您需要转换std::sort为正确的函数指针类型,或者以某种方式包装它,让编译器确定您std::sort()正在尝试调用哪个版本。

处理这个问题的最简单方法是使用 lambda 表达式来包装调用,std::sort()以便编译器可以为您执行模板推导。看起来像这样:

std::thread t1([&](){ std::sort(data.begin() + data.size() / 2, data.end()); });
Run Code Online (Sandbox Code Playgroud)