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

46qrfjad  于 2023-01-28  发布在  其他
关注(0)|答案(3)|浏览(157)

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

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

我的想法是让每个线程对向量的一半进行排序,然后合并它:

#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";
}

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

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

预期输出:

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

9lowa7mx1#

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

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

j2cgzkjk2#

std::sort()是一个函数模板,因此不能按原样传递给需要具体函子(行为类似函数的类型)的函数。您需要将std::sort强制转换为正确的函数指针类型,或者以某种方式将其 Package ,以便编译器确定您试图调用的std::sort()的版本。
最简单的处理方法是使用lambda表达式来 Package 对std::sort()的调用,这样编译器就可以为您进行模板推导,如下所示:

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

5vf7fwbs3#

您可以使用lambda:

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

相关问题